[gdb/testsuite] Use unique portnum in parallel testing
[binutils-gdb.git] / gdb / eval.c
blob6b752e7063552eb2aa9769127adb19400ecbbdec
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"));
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 SEARCH_FUNCTION_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 const symbol_computed_ops *computed_ops = sym->computed_ops ();
1073 if (computed_ops == nullptr
1074 || computed_ops->read_variable_at_entry == nullptr)
1075 error (_("Symbol \"%s\" does not have any specific entry value"),
1076 sym->print_name ());
1078 frame_info_ptr frame = get_selected_frame (NULL);
1079 return computed_ops->read_variable_at_entry (sym, frame);
1082 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1084 struct value *
1085 eval_op_var_msym_value (struct type *expect_type, struct expression *exp,
1086 enum noside noside, bool outermost_p,
1087 bound_minimal_symbol msymbol)
1089 value *val = evaluate_var_msym_value (noside, msymbol.objfile,
1090 msymbol.minsym);
1092 struct type *type = val->type ();
1093 if (type->code () == TYPE_CODE_ERROR
1094 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p))
1095 error_unknown_type (msymbol.minsym->print_name ());
1096 return val;
1099 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1101 struct value *
1102 eval_op_func_static_var (struct type *expect_type, struct expression *exp,
1103 enum noside noside,
1104 value *func, const char *var)
1106 CORE_ADDR addr = func->address ();
1107 const block *blk = block_for_pc (addr);
1108 struct block_symbol sym = lookup_symbol (var, blk, SEARCH_VAR_DOMAIN,
1109 nullptr);
1110 if (sym.symbol == NULL)
1111 error (_("No symbol \"%s\" in specified context."), var);
1112 return evaluate_var_value (noside, sym.block, sym.symbol);
1115 /* Helper function that implements the body of OP_REGISTER. */
1117 struct value *
1118 eval_op_register (struct type *expect_type, struct expression *exp,
1119 enum noside noside, const char *name)
1121 int regno;
1122 struct value *val;
1124 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1125 name, strlen (name));
1126 if (regno == -1)
1127 error (_("Register $%s not available."), name);
1129 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1130 a value with the appropriate register type. Unfortunately,
1131 we don't have easy access to the type of user registers.
1132 So for these registers, we fetch the register value regardless
1133 of the evaluation mode. */
1134 if (noside == EVAL_AVOID_SIDE_EFFECTS
1135 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
1136 val = value::zero (register_type (exp->gdbarch, regno), not_lval);
1137 else
1138 val = value_of_register
1139 (regno, get_next_frame_sentinel_okay (get_selected_frame ()));
1140 if (val == NULL)
1141 error (_("Value of register %s not available."), name);
1142 else
1143 return val;
1146 namespace expr
1149 value *
1150 string_operation::evaluate (struct type *expect_type,
1151 struct expression *exp,
1152 enum noside noside)
1154 const std::string &str = std::get<0> (m_storage);
1155 struct type *type = language_string_char_type (exp->language_defn,
1156 exp->gdbarch);
1157 return value_string (str.c_str (), str.size (), type);
1160 struct value *
1161 ternop_slice_operation::evaluate (struct type *expect_type,
1162 struct expression *exp,
1163 enum noside noside)
1165 struct value *array
1166 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1167 struct value *low
1168 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1169 struct value *upper
1170 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
1172 int lowbound = value_as_long (low);
1173 int upperbound = value_as_long (upper);
1174 return value_slice (array, lowbound, upperbound - lowbound + 1);
1177 } /* namespace expr */
1179 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1181 struct value *
1182 eval_op_objc_selector (struct type *expect_type, struct expression *exp,
1183 enum noside noside,
1184 const char *sel)
1186 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1187 return value_from_longest (selector_type,
1188 lookup_child_selector (exp->gdbarch, sel));
1191 /* A helper function for STRUCTOP_STRUCT. */
1193 struct value *
1194 eval_op_structop_struct (struct type *expect_type, struct expression *exp,
1195 enum noside noside,
1196 struct value *arg1, const char *string)
1198 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1199 NULL, "structure");
1200 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1201 arg3 = value::zero (arg3->type (), arg3->lval ());
1202 return arg3;
1205 /* A helper function for STRUCTOP_PTR. */
1207 struct value *
1208 eval_op_structop_ptr (struct type *expect_type, struct expression *exp,
1209 enum noside noside,
1210 struct value *arg1, const char *string)
1212 /* Check to see if operator '->' has been overloaded. If so replace
1213 arg1 with the value returned by evaluating operator->(). */
1214 while (unop_user_defined_p (STRUCTOP_PTR, arg1))
1216 struct value *value = NULL;
1219 value = value_x_unop (arg1, STRUCTOP_PTR, noside);
1222 catch (const gdb_exception_error &except)
1224 if (except.error == NOT_FOUND_ERROR)
1225 break;
1226 else
1227 throw;
1230 arg1 = value;
1233 /* JYG: if print object is on we need to replace the base type
1234 with rtti type in order to continue on with successful
1235 lookup of member / method only available in the rtti type. */
1237 struct type *arg_type = arg1->type ();
1238 struct type *real_type;
1239 int full, using_enc;
1240 LONGEST top;
1241 struct value_print_options opts;
1243 get_user_print_options (&opts);
1244 if (opts.objectprint && arg_type->target_type ()
1245 && (arg_type->target_type ()->code () == TYPE_CODE_STRUCT))
1247 real_type = value_rtti_indirect_type (arg1, &full, &top,
1248 &using_enc);
1249 if (real_type)
1250 arg1 = value_cast (real_type, arg1);
1254 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1255 NULL, "structure pointer");
1256 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1257 arg3 = value::zero (arg3->type (), arg3->lval ());
1258 return arg3;
1261 /* A helper function for STRUCTOP_MEMBER. */
1263 struct value *
1264 eval_op_member (struct type *expect_type, struct expression *exp,
1265 enum noside noside,
1266 struct value *arg1, struct value *arg2)
1268 long mem_offset;
1270 struct value *arg3;
1271 struct type *type = check_typedef (arg2->type ());
1272 switch (type->code ())
1274 case TYPE_CODE_METHODPTR:
1275 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1276 return value::zero (type->target_type (), not_lval);
1277 else
1279 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1280 gdb_assert (arg2->type ()->code () == TYPE_CODE_PTR);
1281 return value_ind (arg2);
1284 case TYPE_CODE_MEMBERPTR:
1285 /* Now, convert these values to an address. */
1286 if (check_typedef (arg1->type ())->code () != TYPE_CODE_PTR)
1287 arg1 = value_addr (arg1);
1288 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1289 arg1, 1);
1291 mem_offset = value_as_long (arg2);
1293 arg3 = value_from_pointer (lookup_pointer_type (type->target_type ()),
1294 value_as_long (arg1) + mem_offset);
1295 return value_ind (arg3);
1297 default:
1298 error (_("non-pointer-to-member value used "
1299 "in pointer-to-member construct"));
1303 /* A helper function for BINOP_ADD. */
1305 struct value *
1306 eval_op_add (struct type *expect_type, struct expression *exp,
1307 enum noside noside,
1308 struct value *arg1, struct value *arg2)
1310 if (binop_user_defined_p (BINOP_ADD, arg1, arg2))
1311 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside);
1312 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1313 && is_integral_or_integral_reference (arg2->type ()))
1314 return value_ptradd (arg1, value_as_long (arg2));
1315 else if (ptrmath_type_p (exp->language_defn, arg2->type ())
1316 && is_integral_or_integral_reference (arg1->type ()))
1317 return value_ptradd (arg2, value_as_long (arg1));
1318 else
1320 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1321 return value_binop (arg1, arg2, BINOP_ADD);
1325 /* A helper function for BINOP_SUB. */
1327 struct value *
1328 eval_op_sub (struct type *expect_type, struct expression *exp,
1329 enum noside noside,
1330 struct value *arg1, struct value *arg2)
1332 if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
1333 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
1334 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1335 && ptrmath_type_p (exp->language_defn, arg2->type ()))
1337 /* FIXME -- should be ptrdiff_t */
1338 struct type *type = builtin_type (exp->gdbarch)->builtin_long;
1339 return value_from_longest (type, value_ptrdiff (arg1, arg2));
1341 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1342 && is_integral_or_integral_reference (arg2->type ()))
1343 return value_ptradd (arg1, - value_as_long (arg2));
1344 else
1346 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1347 return value_binop (arg1, arg2, BINOP_SUB);
1351 /* Helper function for several different binary operations. */
1353 struct value *
1354 eval_op_binary (struct type *expect_type, struct expression *exp,
1355 enum noside noside, enum exp_opcode op,
1356 struct value *arg1, struct value *arg2)
1358 if (binop_user_defined_p (op, arg1, arg2))
1359 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1360 else
1362 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1363 fudge arg2 to avoid division-by-zero, the caller is
1364 (theoretically) only looking for the type of the result. */
1365 if (noside == EVAL_AVOID_SIDE_EFFECTS
1366 /* ??? Do we really want to test for BINOP_MOD here?
1367 The implementation of value_binop gives it a well-defined
1368 value. */
1369 && (op == BINOP_DIV
1370 || op == BINOP_INTDIV
1371 || op == BINOP_REM
1372 || op == BINOP_MOD)
1373 && value_logical_not (arg2))
1375 struct value *v_one;
1377 v_one = value_one (arg2->type ());
1378 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
1379 return value_binop (arg1, v_one, op);
1381 else
1383 /* For shift and integer exponentiation operations,
1384 only promote the first argument. */
1385 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1386 && is_integral_type (arg2->type ()))
1387 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1388 else
1389 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1391 return value_binop (arg1, arg2, op);
1396 /* A helper function for BINOP_SUBSCRIPT. */
1398 struct value *
1399 eval_op_subscript (struct type *expect_type, struct expression *exp,
1400 enum noside noside, enum exp_opcode op,
1401 struct value *arg1, struct value *arg2)
1403 if (binop_user_defined_p (op, arg1, arg2))
1404 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1405 else
1407 /* If the user attempts to subscript something that is not an
1408 array or pointer type (like a plain int variable for example),
1409 then report this as an error. */
1411 arg1 = coerce_ref (arg1);
1412 struct type *type = check_typedef (arg1->type ());
1413 if (type->code () != TYPE_CODE_ARRAY
1414 && type->code () != TYPE_CODE_PTR)
1416 if (type->name ())
1417 error (_("cannot subscript something of type `%s'"),
1418 type->name ());
1419 else
1420 error (_("cannot subscript requested type"));
1423 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1424 return value::zero (type->target_type (), arg1->lval ());
1425 else
1426 return value_subscript (arg1, value_as_long (arg2));
1430 /* A helper function for BINOP_EQUAL. */
1432 struct value *
1433 eval_op_equal (struct type *expect_type, struct expression *exp,
1434 enum noside noside, enum exp_opcode op,
1435 struct value *arg1, struct value *arg2)
1437 if (binop_user_defined_p (op, arg1, arg2))
1439 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1441 else
1443 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1444 int tem = value_equal (arg1, arg2);
1445 struct type *type = language_bool_type (exp->language_defn,
1446 exp->gdbarch);
1447 return value_from_longest (type, (LONGEST) tem);
1451 /* A helper function for BINOP_NOTEQUAL. */
1453 struct value *
1454 eval_op_notequal (struct type *expect_type, struct expression *exp,
1455 enum noside noside, enum exp_opcode op,
1456 struct value *arg1, struct value *arg2)
1458 if (binop_user_defined_p (op, arg1, arg2))
1460 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1462 else
1464 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1465 int tem = value_equal (arg1, arg2);
1466 struct type *type = language_bool_type (exp->language_defn,
1467 exp->gdbarch);
1468 return value_from_longest (type, (LONGEST) ! tem);
1472 /* A helper function for BINOP_LESS. */
1474 struct value *
1475 eval_op_less (struct type *expect_type, struct expression *exp,
1476 enum noside noside, enum exp_opcode op,
1477 struct value *arg1, struct value *arg2)
1479 if (binop_user_defined_p (op, arg1, arg2))
1481 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1483 else
1485 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1486 int tem = value_less (arg1, arg2);
1487 struct type *type = language_bool_type (exp->language_defn,
1488 exp->gdbarch);
1489 return value_from_longest (type, (LONGEST) tem);
1493 /* A helper function for BINOP_GTR. */
1495 struct value *
1496 eval_op_gtr (struct type *expect_type, struct expression *exp,
1497 enum noside noside, enum exp_opcode op,
1498 struct value *arg1, struct value *arg2)
1500 if (binop_user_defined_p (op, arg1, arg2))
1502 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1504 else
1506 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1507 int tem = value_less (arg2, arg1);
1508 struct type *type = language_bool_type (exp->language_defn,
1509 exp->gdbarch);
1510 return value_from_longest (type, (LONGEST) tem);
1514 /* A helper function for BINOP_GEQ. */
1516 struct value *
1517 eval_op_geq (struct type *expect_type, struct expression *exp,
1518 enum noside noside, enum exp_opcode op,
1519 struct value *arg1, struct value *arg2)
1521 if (binop_user_defined_p (op, arg1, arg2))
1523 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1525 else
1527 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1528 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1529 struct type *type = language_bool_type (exp->language_defn,
1530 exp->gdbarch);
1531 return value_from_longest (type, (LONGEST) tem);
1535 /* A helper function for BINOP_LEQ. */
1537 struct value *
1538 eval_op_leq (struct type *expect_type, struct expression *exp,
1539 enum noside noside, enum exp_opcode op,
1540 struct value *arg1, struct value *arg2)
1542 if (binop_user_defined_p (op, arg1, arg2))
1544 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1546 else
1548 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1549 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1550 struct type *type = language_bool_type (exp->language_defn,
1551 exp->gdbarch);
1552 return value_from_longest (type, (LONGEST) tem);
1556 /* A helper function for BINOP_REPEAT. */
1558 struct value *
1559 eval_op_repeat (struct type *expect_type, struct expression *exp,
1560 enum noside noside, enum exp_opcode op,
1561 struct value *arg1, struct value *arg2)
1563 struct type *type = check_typedef (arg2->type ());
1564 if (type->code () != TYPE_CODE_INT
1565 && type->code () != TYPE_CODE_ENUM)
1566 error (_("Non-integral right operand for \"@\" operator."));
1567 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1569 return allocate_repeat_value (arg1->type (),
1570 longest_to_int (value_as_long (arg2)));
1572 else
1573 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1576 /* A helper function for UNOP_PLUS. */
1578 struct value *
1579 eval_op_plus (struct type *expect_type, struct expression *exp,
1580 enum noside noside, enum exp_opcode op,
1581 struct value *arg1)
1583 if (unop_user_defined_p (op, arg1))
1584 return value_x_unop (arg1, op, noside);
1585 else
1587 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1588 return value_pos (arg1);
1592 /* A helper function for UNOP_NEG. */
1594 struct value *
1595 eval_op_neg (struct type *expect_type, struct expression *exp,
1596 enum noside noside, enum exp_opcode op,
1597 struct value *arg1)
1599 if (unop_user_defined_p (op, arg1))
1600 return value_x_unop (arg1, op, noside);
1601 else
1603 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1604 return value_neg (arg1);
1608 /* A helper function for UNOP_COMPLEMENT. */
1610 struct value *
1611 eval_op_complement (struct type *expect_type, struct expression *exp,
1612 enum noside noside, enum exp_opcode op,
1613 struct value *arg1)
1615 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1616 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1617 else
1619 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1620 return value_complement (arg1);
1624 /* A helper function for UNOP_LOGICAL_NOT. */
1626 struct value *
1627 eval_op_lognot (struct type *expect_type, struct expression *exp,
1628 enum noside noside, enum exp_opcode op,
1629 struct value *arg1)
1631 if (unop_user_defined_p (op, arg1))
1632 return value_x_unop (arg1, op, noside);
1633 else
1635 struct type *type = language_bool_type (exp->language_defn,
1636 exp->gdbarch);
1637 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
1641 /* A helper function for UNOP_IND. */
1643 struct value *
1644 eval_op_ind (struct type *expect_type, struct expression *exp,
1645 enum noside noside,
1646 struct value *arg1)
1648 struct type *type = check_typedef (arg1->type ());
1649 if (type->code () == TYPE_CODE_METHODPTR
1650 || type->code () == TYPE_CODE_MEMBERPTR)
1651 error (_("Attempt to dereference pointer "
1652 "to member without an object"));
1653 if (unop_user_defined_p (UNOP_IND, arg1))
1654 return value_x_unop (arg1, UNOP_IND, noside);
1655 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1657 type = check_typedef (arg1->type ());
1659 /* If the type pointed to is dynamic then in order to resolve the
1660 dynamic properties we must actually dereference the pointer.
1661 There is a risk that this dereference will have side-effects
1662 in the inferior, but being able to print accurate type
1663 information seems worth the risk. */
1664 if (!type->is_pointer_or_reference ()
1665 || !is_dynamic_type (type->target_type ()))
1667 if (type->is_pointer_or_reference ()
1668 /* In C you can dereference an array to get the 1st elt. */
1669 || type->code () == TYPE_CODE_ARRAY)
1670 return value::zero (type->target_type (),
1671 lval_memory);
1672 else if (type->code () == TYPE_CODE_INT)
1673 /* GDB allows dereferencing an int. */
1674 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
1675 lval_memory);
1676 else
1677 error (_("Attempt to take contents of a non-pointer value."));
1681 /* Allow * on an integer so we can cast it to whatever we want.
1682 This returns an int, which seems like the most C-like thing to
1683 do. "long long" variables are rare enough that
1684 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1685 if (type->code () == TYPE_CODE_INT)
1686 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
1687 value_as_address (arg1));
1688 return value_ind (arg1);
1691 /* A helper function for UNOP_ALIGNOF. */
1693 struct value *
1694 eval_op_alignof (struct type *expect_type, struct expression *exp,
1695 enum noside noside,
1696 struct value *arg1)
1698 struct type *type = arg1->type ();
1699 /* FIXME: This should be size_t. */
1700 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
1701 ULONGEST align = type_align (type);
1702 if (align == 0)
1703 error (_("could not determine alignment of type"));
1704 return value_from_longest (size_type, align);
1707 /* A helper function for UNOP_MEMVAL. */
1709 struct value *
1710 eval_op_memval (struct type *expect_type, struct expression *exp,
1711 enum noside noside,
1712 struct value *arg1, struct type *type)
1714 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1715 return value::zero (type, lval_memory);
1716 else
1717 return value_at_lazy (type, value_as_address (arg1));
1720 /* A helper function for UNOP_PREINCREMENT. */
1722 struct value *
1723 eval_op_preinc (struct type *expect_type, struct expression *exp,
1724 enum noside noside, enum exp_opcode op,
1725 struct value *arg1)
1727 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1728 return arg1;
1729 else if (unop_user_defined_p (op, arg1))
1731 return value_x_unop (arg1, op, noside);
1733 else
1735 struct value *arg2;
1736 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1737 arg2 = value_ptradd (arg1, 1);
1738 else
1740 struct value *tmp = arg1;
1742 arg2 = value_one (arg1->type ());
1743 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1744 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1747 return value_assign (arg1, arg2);
1751 /* A helper function for UNOP_PREDECREMENT. */
1753 struct value *
1754 eval_op_predec (struct type *expect_type, struct expression *exp,
1755 enum noside noside, enum exp_opcode op,
1756 struct value *arg1)
1758 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1759 return arg1;
1760 else if (unop_user_defined_p (op, arg1))
1762 return value_x_unop (arg1, op, noside);
1764 else
1766 struct value *arg2;
1767 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1768 arg2 = value_ptradd (arg1, -1);
1769 else
1771 struct value *tmp = arg1;
1773 arg2 = value_one (arg1->type ());
1774 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1775 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1778 return value_assign (arg1, arg2);
1782 /* A helper function for UNOP_POSTINCREMENT. */
1784 struct value *
1785 eval_op_postinc (struct type *expect_type, struct expression *exp,
1786 enum noside noside, enum exp_opcode op,
1787 struct value *arg1)
1789 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1790 return arg1;
1791 else if (unop_user_defined_p (op, arg1))
1793 return value_x_unop (arg1, op, noside);
1795 else
1797 struct value *arg3 = arg1->non_lval ();
1798 struct value *arg2;
1800 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1801 arg2 = value_ptradd (arg1, 1);
1802 else
1804 struct value *tmp = arg1;
1806 arg2 = value_one (arg1->type ());
1807 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1808 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1811 value_assign (arg1, arg2);
1812 return arg3;
1816 /* A helper function for UNOP_POSTDECREMENT. */
1818 struct value *
1819 eval_op_postdec (struct type *expect_type, struct expression *exp,
1820 enum noside noside, enum exp_opcode op,
1821 struct value *arg1)
1823 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1824 return arg1;
1825 else if (unop_user_defined_p (op, arg1))
1827 return value_x_unop (arg1, op, noside);
1829 else
1831 struct value *arg3 = arg1->non_lval ();
1832 struct value *arg2;
1834 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1835 arg2 = value_ptradd (arg1, -1);
1836 else
1838 struct value *tmp = arg1;
1840 arg2 = value_one (arg1->type ());
1841 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1842 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1845 value_assign (arg1, arg2);
1846 return arg3;
1850 /* A helper function for OP_TYPE. */
1852 struct value *
1853 eval_op_type (struct type *expect_type, struct expression *exp,
1854 enum noside noside, struct type *type)
1856 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1857 return value::allocate (type);
1858 else
1859 error (_("Attempt to use a type name as an expression"));
1862 /* A helper function for BINOP_ASSIGN_MODIFY. */
1864 struct value *
1865 eval_binop_assign_modify (struct type *expect_type, struct expression *exp,
1866 enum noside noside, enum exp_opcode op,
1867 struct value *arg1, struct value *arg2)
1869 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1870 return arg1;
1871 if (binop_user_defined_p (op, arg1, arg2))
1872 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1873 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1874 arg1->type ())
1875 && is_integral_type (arg2->type ()))
1876 arg2 = value_ptradd (arg1, value_as_long (arg2));
1877 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
1878 arg1->type ())
1879 && is_integral_type (arg2->type ()))
1880 arg2 = value_ptradd (arg1, - value_as_long (arg2));
1881 else
1883 struct value *tmp = arg1;
1885 /* For shift and integer exponentiation operations,
1886 only promote the first argument. */
1887 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1888 && is_integral_type (arg2->type ()))
1889 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1890 else
1891 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1893 arg2 = value_binop (tmp, arg2, op);
1895 return value_assign (arg1, arg2);
1898 /* Note that ARGS needs 2 empty slots up front and must end with a
1899 null pointer. */
1900 static struct value *
1901 eval_op_objc_msgcall (struct type *expect_type, struct expression *exp,
1902 enum noside noside, CORE_ADDR selector,
1903 value *target, gdb::array_view<value *> args)
1905 CORE_ADDR responds_selector = 0;
1906 CORE_ADDR method_selector = 0;
1908 int struct_return = 0;
1910 struct value *msg_send = NULL;
1911 struct value *msg_send_stret = NULL;
1912 int gnu_runtime = 0;
1914 struct value *method = NULL;
1915 struct value *called_method = NULL;
1917 struct type *selector_type = NULL;
1918 struct type *long_type;
1919 struct type *type;
1921 struct value *ret = NULL;
1922 CORE_ADDR addr = 0;
1924 value *argvec[5];
1926 long_type = builtin_type (exp->gdbarch)->builtin_long;
1927 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1929 if (value_as_long (target) == 0)
1930 return value_from_longest (long_type, 0);
1932 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1933 gnu_runtime = 1;
1935 /* Find the method dispatch (Apple runtime) or method lookup
1936 (GNU runtime) function for Objective-C. These will be used
1937 to lookup the symbol information for the method. If we
1938 can't find any symbol information, then we'll use these to
1939 call the method, otherwise we can call the method
1940 directly. The msg_send_stret function is used in the special
1941 case of a method that returns a structure (Apple runtime
1942 only). */
1943 if (gnu_runtime)
1945 type = selector_type;
1947 type = lookup_function_type (type);
1948 type = lookup_pointer_type (type);
1949 type = lookup_function_type (type);
1950 type = lookup_pointer_type (type);
1952 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1953 msg_send_stret
1954 = find_function_in_inferior ("objc_msg_lookup", NULL);
1956 msg_send = value_from_pointer (type, value_as_address (msg_send));
1957 msg_send_stret = value_from_pointer (type,
1958 value_as_address (msg_send_stret));
1960 else
1962 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1963 /* Special dispatcher for methods returning structs. */
1964 msg_send_stret
1965 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1968 /* Verify the target object responds to this method. The
1969 standard top-level 'Object' class uses a different name for
1970 the verification method than the non-standard, but more
1971 often used, 'NSObject' class. Make sure we check for both. */
1973 responds_selector
1974 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1975 if (responds_selector == 0)
1976 responds_selector
1977 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1979 if (responds_selector == 0)
1980 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1982 method_selector
1983 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1984 if (method_selector == 0)
1985 method_selector
1986 = lookup_child_selector (exp->gdbarch, "methodFor:");
1988 if (method_selector == 0)
1989 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1991 /* Call the verification method, to make sure that the target
1992 class implements the desired method. */
1994 argvec[0] = msg_send;
1995 argvec[1] = target;
1996 argvec[2] = value_from_longest (long_type, responds_selector);
1997 argvec[3] = value_from_longest (long_type, selector);
1998 argvec[4] = 0;
2000 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2001 if (gnu_runtime)
2003 /* Function objc_msg_lookup returns a pointer. */
2004 argvec[0] = ret;
2005 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2007 if (value_as_long (ret) == 0)
2008 error (_("Target does not respond to this message selector."));
2010 /* Call "methodForSelector:" method, to get the address of a
2011 function method that implements this selector for this
2012 class. If we can find a symbol at that address, then we
2013 know the return type, parameter types etc. (that's a good
2014 thing). */
2016 argvec[0] = msg_send;
2017 argvec[1] = target;
2018 argvec[2] = value_from_longest (long_type, method_selector);
2019 argvec[3] = value_from_longest (long_type, selector);
2020 argvec[4] = 0;
2022 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2023 if (gnu_runtime)
2025 argvec[0] = ret;
2026 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2029 /* ret should now be the selector. */
2031 addr = value_as_long (ret);
2032 if (addr)
2034 struct symbol *sym = NULL;
2036 /* The address might point to a function descriptor;
2037 resolve it to the actual code address instead. */
2038 addr = gdbarch_convert_from_func_ptr_addr
2039 (exp->gdbarch, addr, current_inferior ()->top_target ());
2041 /* Is it a high_level symbol? */
2042 sym = find_pc_function (addr);
2043 if (sym != NULL)
2044 method = value_of_variable (sym, 0);
2047 /* If we found a method with symbol information, check to see
2048 if it returns a struct. Otherwise assume it doesn't. */
2050 if (method)
2052 CORE_ADDR funaddr;
2053 struct type *val_type;
2055 funaddr = find_function_addr (method, &val_type);
2057 block_for_pc (funaddr);
2059 val_type = check_typedef (val_type);
2061 if ((val_type == NULL)
2062 || (val_type->code () == TYPE_CODE_ERROR))
2064 if (expect_type != NULL)
2065 val_type = expect_type;
2068 struct_return = using_struct_return (exp->gdbarch, method,
2069 val_type);
2071 else if (expect_type != NULL)
2073 struct_return = using_struct_return (exp->gdbarch, NULL,
2074 check_typedef (expect_type));
2077 /* Found a function symbol. Now we will substitute its
2078 value in place of the message dispatcher (obj_msgSend),
2079 so that we call the method directly instead of thru
2080 the dispatcher. The main reason for doing this is that
2081 we can now evaluate the return value and parameter values
2082 according to their known data types, in case we need to
2083 do things like promotion, dereferencing, special handling
2084 of structs and doubles, etc.
2086 We want to use the type signature of 'method', but still
2087 jump to objc_msgSend() or objc_msgSend_stret() to better
2088 mimic the behavior of the runtime. */
2090 if (method)
2092 if (method->type ()->code () != TYPE_CODE_FUNC)
2093 error (_("method address has symbol information "
2094 "with non-function type; skipping"));
2096 /* Create a function pointer of the appropriate type, and
2097 replace its value with the value of msg_send or
2098 msg_send_stret. We must use a pointer here, as
2099 msg_send and msg_send_stret are of pointer type, and
2100 the representation may be different on systems that use
2101 function descriptors. */
2102 if (struct_return)
2103 called_method
2104 = value_from_pointer (lookup_pointer_type (method->type ()),
2105 value_as_address (msg_send_stret));
2106 else
2107 called_method
2108 = value_from_pointer (lookup_pointer_type (method->type ()),
2109 value_as_address (msg_send));
2111 else
2113 if (struct_return)
2114 called_method = msg_send_stret;
2115 else
2116 called_method = msg_send;
2120 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2122 /* If the return type doesn't look like a function type,
2123 call an error. This can happen if somebody tries to
2124 turn a variable into a function call. This is here
2125 because people often want to call, eg, strcmp, which
2126 gdb doesn't know is a function. If gdb isn't asked for
2127 it's opinion (ie. through "whatis"), it won't offer
2128 it. */
2130 struct type *callee_type = called_method->type ();
2132 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
2133 callee_type = callee_type->target_type ();
2134 callee_type = callee_type->target_type ();
2136 if (callee_type)
2138 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
2139 return value::allocate (expect_type);
2140 else
2141 return value::allocate (callee_type);
2143 else
2144 error (_("Expression of type other than "
2145 "\"method returning ...\" used as a method"));
2148 /* Now depending on whether we found a symbol for the method,
2149 we will either call the runtime dispatcher or the method
2150 directly. */
2152 args[0] = target;
2153 args[1] = value_from_longest (long_type, selector);
2155 if (gnu_runtime && (method != NULL))
2157 /* Function objc_msg_lookup returns a pointer. */
2158 struct type *tem_type = called_method->type ();
2159 tem_type = lookup_pointer_type (lookup_function_type (tem_type));
2160 called_method->deprecated_set_type (tem_type);
2161 called_method = call_function_by_hand (called_method, NULL, args);
2164 return call_function_by_hand (called_method, NULL, args);
2167 /* Helper function for MULTI_SUBSCRIPT. */
2169 static struct value *
2170 eval_multi_subscript (struct type *expect_type, struct expression *exp,
2171 enum noside noside, value *arg1,
2172 gdb::array_view<value *> args)
2174 for (value *arg2 : args)
2176 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2))
2178 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside);
2180 else
2182 arg1 = coerce_ref (arg1);
2183 struct type *type = check_typedef (arg1->type ());
2185 switch (type->code ())
2187 case TYPE_CODE_PTR:
2188 case TYPE_CODE_ARRAY:
2189 case TYPE_CODE_STRING:
2190 arg1 = value_subscript (arg1, value_as_long (arg2));
2191 break;
2193 default:
2194 if (type->name ())
2195 error (_("cannot subscript something of type `%s'"),
2196 type->name ());
2197 else
2198 error (_("cannot subscript requested type"));
2202 return (arg1);
2205 namespace expr
2208 value *
2209 objc_msgcall_operation::evaluate (struct type *expect_type,
2210 struct expression *exp,
2211 enum noside noside)
2213 enum noside sub_no_side = EVAL_NORMAL;
2214 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2216 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2217 sub_no_side = EVAL_NORMAL;
2218 else
2219 sub_no_side = noside;
2220 value *target
2221 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side);
2223 if (value_as_long (target) == 0)
2224 sub_no_side = EVAL_AVOID_SIDE_EFFECTS;
2225 else
2226 sub_no_side = noside;
2227 std::vector<operation_up> &args = std::get<2> (m_storage);
2228 value **argvec = XALLOCAVEC (struct value *, args.size () + 3);
2229 argvec[0] = nullptr;
2230 argvec[1] = nullptr;
2231 for (int i = 0; i < args.size (); ++i)
2232 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side);
2233 argvec[args.size () + 2] = nullptr;
2235 return eval_op_objc_msgcall (expect_type, exp, noside, std::
2236 get<0> (m_storage), target,
2237 gdb::make_array_view (argvec,
2238 args.size () + 3));
2241 value *
2242 multi_subscript_operation::evaluate (struct type *expect_type,
2243 struct expression *exp,
2244 enum noside noside)
2246 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
2247 std::vector<operation_up> &values = std::get<1> (m_storage);
2248 value **argvec = XALLOCAVEC (struct value *, values.size ());
2249 for (int ix = 0; ix < values.size (); ++ix)
2250 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside);
2251 return eval_multi_subscript (expect_type, exp, noside, arg1,
2252 gdb::make_array_view (argvec, values.size ()));
2255 value *
2256 logical_and_operation::evaluate (struct type *expect_type,
2257 struct expression *exp,
2258 enum noside noside)
2260 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2262 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2263 EVAL_AVOID_SIDE_EFFECTS);
2265 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2))
2267 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2268 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside);
2270 else
2272 bool tem = value_logical_not (arg1);
2273 if (!tem)
2275 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2276 tem = value_logical_not (arg2);
2278 struct type *type = language_bool_type (exp->language_defn,
2279 exp->gdbarch);
2280 return value_from_longest (type, !tem);
2284 value *
2285 logical_or_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_OR, arg1, arg2))
2296 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2297 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, 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);
2308 struct type *type = language_bool_type (exp->language_defn,
2309 exp->gdbarch);
2310 return value_from_longest (type, !tem);
2314 value *
2315 adl_func_operation::evaluate (struct type *expect_type,
2316 struct expression *exp,
2317 enum noside noside)
2319 std::vector<operation_up> &arg_ops = std::get<2> (m_storage);
2320 std::vector<value *> args (arg_ops.size ());
2321 for (int i = 0; i < arg_ops.size (); ++i)
2322 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside);
2324 struct symbol *symp;
2325 find_overload_match (args, std::get<0> (m_storage).c_str (),
2326 NON_METHOD,
2327 nullptr, nullptr,
2328 nullptr, &symp, nullptr, 0, noside);
2329 if (symp->type ()->code () == TYPE_CODE_ERROR)
2330 error_unknown_type (symp->print_name ());
2331 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
2332 return evaluate_subexp_do_call (exp, noside, callee, args,
2333 nullptr, expect_type);
2337 /* This function evaluates brace-initializers (in C/C++) for
2338 structure types. */
2340 struct value *
2341 array_operation::evaluate_struct_tuple (struct value *struct_val,
2342 struct expression *exp,
2343 enum noside noside, int nargs)
2345 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2346 struct type *struct_type = check_typedef (struct_val->type ());
2347 struct type *field_type;
2348 int fieldno = -1;
2350 int idx = 0;
2351 while (--nargs >= 0)
2353 struct value *val = NULL;
2354 int bitpos, bitsize;
2355 bfd_byte *addr;
2357 fieldno++;
2358 /* Skip static fields. */
2359 while (fieldno < struct_type->num_fields ()
2360 && struct_type->field (fieldno).is_static ())
2361 fieldno++;
2362 if (fieldno >= struct_type->num_fields ())
2363 error (_("too many initializers"));
2364 field_type = struct_type->field (fieldno).type ();
2365 if (field_type->code () == TYPE_CODE_UNION
2366 && struct_type->field (fieldno).name ()[0] == '0')
2367 error (_("don't know which variant you want to set"));
2369 /* Here, struct_type is the type of the inner struct,
2370 while substruct_type is the type of the inner struct.
2371 These are the same for normal structures, but a variant struct
2372 contains anonymous union fields that contain substruct fields.
2373 The value fieldno is the index of the top-level (normal or
2374 anonymous union) field in struct_field, while the value
2375 subfieldno is the index of the actual real (named inner) field
2376 in substruct_type. */
2378 field_type = struct_type->field (fieldno).type ();
2379 if (val == 0)
2380 val = in_args[idx++]->evaluate (field_type, exp, noside);
2382 /* Now actually set the field in struct_val. */
2384 /* Assign val to field fieldno. */
2385 if (val->type () != field_type)
2386 val = value_cast (field_type, val);
2388 bitsize = struct_type->field (fieldno).bitsize ();
2389 bitpos = struct_type->field (fieldno).loc_bitpos ();
2390 addr = struct_val->contents_writeable ().data () + bitpos / 8;
2391 if (bitsize)
2392 modify_field (struct_type, addr,
2393 value_as_long (val), bitpos % 8, bitsize);
2394 else
2395 memcpy (addr, val->contents ().data (),
2396 val->type ()->length ());
2399 return struct_val;
2402 value *
2403 array_operation::evaluate (struct type *expect_type,
2404 struct expression *exp,
2405 enum noside noside)
2407 const int provided_low_bound = std::get<0> (m_storage);
2408 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2409 const int nargs = std::get<1> (m_storage) - provided_low_bound + 1;
2410 struct type *type = expect_type ? check_typedef (expect_type) : nullptr;
2412 if (expect_type != nullptr
2413 && type->code () == TYPE_CODE_STRUCT)
2415 struct value *rec = value::allocate (expect_type);
2417 memset (rec->contents_raw ().data (), '\0', type->length ());
2418 return evaluate_struct_tuple (rec, exp, noside, nargs);
2421 if (expect_type != nullptr
2422 && type->code () == TYPE_CODE_ARRAY)
2424 struct type *range_type = type->index_type ();
2425 struct type *element_type = type->target_type ();
2426 struct value *array = value::allocate (expect_type);
2427 int element_size = check_typedef (element_type)->length ();
2428 LONGEST low_bound, high_bound;
2430 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
2432 low_bound = 0;
2433 high_bound = (type->length () / element_size) - 1;
2435 if (low_bound + nargs - 1 > high_bound)
2436 error (_("Too many array elements"));
2437 memset (array->contents_raw ().data (), 0, expect_type->length ());
2438 for (int idx = 0; idx < nargs; ++idx)
2440 struct value *element;
2442 element = in_args[idx]->evaluate (element_type, exp, noside);
2443 if (element->type () != element_type)
2444 element = value_cast (element_type, element);
2445 memcpy (array->contents_raw ().data () + idx * element_size,
2446 element->contents ().data (),
2447 element_size);
2449 return array;
2452 if (expect_type != nullptr
2453 && type->code () == TYPE_CODE_SET)
2455 struct value *set = value::allocate (expect_type);
2456 gdb_byte *valaddr = set->contents_raw ().data ();
2457 struct type *element_type = type->index_type ();
2458 struct type *check_type = element_type;
2459 LONGEST low_bound, high_bound;
2461 /* Get targettype of elementtype. */
2462 while (check_type->code () == TYPE_CODE_RANGE
2463 || check_type->code () == TYPE_CODE_TYPEDEF)
2464 check_type = check_type->target_type ();
2466 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
2467 error (_("(power)set type with unknown size"));
2468 memset (valaddr, '\0', type->length ());
2469 for (int idx = 0; idx < nargs; idx++)
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 std::vector<value *> argvec (nargs);
2518 for (int 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 (provided_low_bound, 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 else if (exp->language_defn->la_language == language_fortran
2711 && type->code () == TYPE_CODE_PTR)
2713 /* Dereference Fortran pointer types to allow them for the Fortran
2714 sizeof intrinsic. */
2715 type = check_typedef (type->target_type ());
2717 return value_from_longest (size_type, (LONGEST) type->length ());
2720 namespace expr
2723 value *
2724 operation::evaluate_for_sizeof (struct expression *exp, enum noside noside)
2726 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
2727 return evaluate_subexp_for_sizeof_base (exp, val->type ());
2730 value *
2731 var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
2732 enum noside noside)
2735 const bound_minimal_symbol &b = std::get<0> (m_storage);
2736 value *mval = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2738 struct type *type = mval->type ();
2739 if (type->code () == TYPE_CODE_ERROR)
2740 error_unknown_type (b.minsym->print_name ());
2742 /* FIXME: This should be size_t. */
2743 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2744 return value_from_longest (size_type, type->length ());
2747 value *
2748 subscript_operation::evaluate_for_sizeof (struct expression *exp,
2749 enum noside noside)
2751 if (noside == EVAL_NORMAL)
2753 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2754 EVAL_AVOID_SIDE_EFFECTS);
2755 struct type *type = check_typedef (val->type ());
2756 if (type->code () == TYPE_CODE_ARRAY)
2758 type = check_typedef (type->target_type ());
2759 if (type->code () == TYPE_CODE_ARRAY)
2761 type = type->index_type ();
2762 /* Only re-evaluate the right hand side if the resulting type
2763 is a variable length type. */
2764 if (type->bounds ()->flag_bound_evaluated)
2766 val = evaluate (nullptr, exp, EVAL_NORMAL);
2767 /* FIXME: This should be size_t. */
2768 struct type *size_type
2769 = builtin_type (exp->gdbarch)->builtin_int;
2770 return value_from_longest
2771 (size_type, (LONGEST) val->type ()->length ());
2777 return operation::evaluate_for_sizeof (exp, noside);
2780 value *
2781 unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
2782 enum noside noside)
2784 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2785 EVAL_AVOID_SIDE_EFFECTS);
2786 struct type *type = check_typedef (val->type ());
2787 if (!type->is_pointer_or_reference ()
2788 && type->code () != TYPE_CODE_ARRAY)
2789 error (_("Attempt to take contents of a non-pointer value."));
2790 type = type->target_type ();
2791 if (is_dynamic_type (type))
2792 type = value_ind (val)->type ();
2793 /* FIXME: This should be size_t. */
2794 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2795 return value_from_longest (size_type, (LONGEST) type->length ());
2798 value *
2799 unop_memval_operation::evaluate_for_sizeof (struct expression *exp,
2800 enum noside noside)
2802 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage));
2805 value *
2806 unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
2807 enum noside noside)
2809 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2810 EVAL_AVOID_SIDE_EFFECTS);
2811 return evaluate_subexp_for_sizeof_base (exp, typeval->type ());
2814 value *
2815 var_value_operation::evaluate_for_sizeof (struct expression *exp,
2816 enum noside noside)
2818 struct type *type = std::get<0> (m_storage).symbol->type ();
2819 if (is_dynamic_type (type))
2821 value *val = evaluate (nullptr, exp, EVAL_NORMAL);
2822 type = val->type ();
2823 if (type->code () == TYPE_CODE_ARRAY)
2825 /* FIXME: This should be size_t. */
2826 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2827 if (type_not_allocated (type) || type_not_associated (type))
2828 return value::zero (size_type, not_lval);
2829 else if (is_dynamic_type (type->index_type ())
2830 && !type->bounds ()->high.is_available ())
2831 return value::allocate_optimized_out (size_type);
2834 return evaluate_subexp_for_sizeof_base (exp, type);
2837 value *
2838 var_msym_value_operation::evaluate_for_cast (struct type *to_type,
2839 struct expression *exp,
2840 enum noside noside)
2842 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2843 return value::zero (to_type, not_lval);
2845 const bound_minimal_symbol &b = std::get<0> (m_storage);
2846 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2848 val = value_cast (to_type, val);
2850 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2851 if (val->lval () == lval_memory)
2853 if (val->lazy ())
2854 val->fetch_lazy ();
2855 val->set_lval (not_lval);
2857 return val;
2860 value *
2861 var_value_operation::evaluate_for_cast (struct type *to_type,
2862 struct expression *exp,
2863 enum noside noside)
2865 value *val = evaluate_var_value (noside,
2866 std::get<0> (m_storage).block,
2867 std::get<0> (m_storage).symbol);
2869 val = value_cast (to_type, val);
2871 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2872 if (val->lval () == lval_memory)
2874 if (val->lazy ())
2875 val->fetch_lazy ();
2876 val->set_lval (not_lval);
2878 return val;
2883 /* Parse a type expression in the string [P..P+LENGTH). */
2885 struct type *
2886 parse_and_eval_type (const char *p, int length)
2888 char *tmp = (char *) alloca (length + 4);
2890 tmp[0] = '(';
2891 memcpy (tmp + 1, p, length);
2892 tmp[length + 1] = ')';
2893 tmp[length + 2] = '0';
2894 tmp[length + 3] = '\0';
2895 expression_up expr = parse_expression (tmp);
2896 expr::unop_cast_operation *op
2897 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ());
2898 if (op == nullptr)
2899 error (_("Internal error in eval_type."));
2900 return op->get_type ();