]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/eval.c
539b700ad8ff08206efde54e6365fe5296aa7b05
[thirdparty/binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2
3 Copyright (C) 1986-2025 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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"
45
46 \f
47 /* Parse the string EXP as a C expression, evaluate it,
48 and return the result as a number. */
49
50 CORE_ADDR
51 parse_and_eval_address (const char *exp)
52 {
53 expression_up expr = parse_expression (exp);
54
55 return value_as_address (expr->evaluate ());
56 }
57
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)
62 {
63 expression_up expr = parse_expression (exp);
64
65 return value_as_long (expr->evaluate ());
66 }
67
68 struct value *
69 parse_and_eval (const char *exp, parser_flags flags)
70 {
71 expression_up expr = parse_expression (exp, nullptr, flags);
72
73 return expr->evaluate ();
74 }
75
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. */
79
80 struct value *
81 parse_to_comma_and_eval (const char **expp)
82 {
83 expression_up expr = parse_exp_1 (expp, 0, nullptr,
84 PARSER_COMMA_TERMINATES);
85
86 return expr->evaluate ();
87 }
88 \f
89
90 /* See expression.h. */
91
92 bool
93 expression::uses_objfile (struct objfile *objfile) const
94 {
95 gdb_assert (objfile->separate_debug_objfile_backlink == nullptr);
96 return op->uses_objfile (objfile);
97 }
98
99 /* See expression.h. */
100
101 struct value *
102 expression::evaluate (struct type *expect_type, enum noside noside)
103 {
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 ());
109
110 struct value *retval = op->evaluate (expect_type, this, noside);
111
112 if (stack_temporaries.has_value ()
113 && value_in_thread_stack_temporaries (retval, inferior_thread ()))
114 retval = retval->non_lval ();
115
116 return retval;
117 }
118
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.
123
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).
131
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.
136
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. */
140
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)
147 {
148 struct value *mark, *new_mark, *result;
149
150 *valp = NULL;
151 if (resultp)
152 *resultp = NULL;
153 if (val_chain)
154 val_chain->clear ();
155
156 /* Evaluate the expression. */
157 mark = value_mark ();
158 result = NULL;
159
160 try
161 {
162 result = op->evaluate (nullptr, exp, EVAL_NORMAL);
163 }
164 catch (const gdb_exception &ex)
165 {
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)
170 {
171 case MEMORY_ERROR:
172 if (!preserve_errors)
173 break;
174 [[fallthrough]];
175 default:
176 throw;
177 break;
178 }
179 }
180
181 new_mark = value_mark ();
182 if (mark == new_mark)
183 return;
184 if (resultp)
185 *resultp = result;
186
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)
190 {
191 if (!result->lazy ())
192 *valp = result;
193 else
194 {
195
196 try
197 {
198 result->fetch_lazy ();
199 *valp = result;
200 }
201 catch (const gdb_exception_error &except)
202 {
203 }
204 }
205 }
206
207 if (val_chain)
208 {
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);
212 }
213 }
214
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. */
219
220 void
221 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
222 struct value **arg1)
223 {
224 struct type *type1;
225
226 *arg1 = coerce_ref (*arg1);
227 type1 = check_typedef ((*arg1)->type ());
228
229 if (is_integral_type (type1))
230 {
231 switch (language->la_language)
232 {
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. */
237 {
238 struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
239
240 if (type1->length () < builtin_int->length ())
241 *arg1 = value_cast (builtin_int, *arg1);
242 }
243 break;
244 }
245 }
246 }
247
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. */
252
253 void
254 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
255 struct value **arg1, struct value **arg2)
256 {
257 struct type *promoted_type = NULL;
258 struct type *type1;
259 struct type *type2;
260
261 *arg1 = coerce_ref (*arg1);
262 *arg2 = coerce_ref (*arg2);
263
264 type1 = check_typedef ((*arg1)->type ());
265 type2 = check_typedef ((*arg2)->type ());
266
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;
274
275 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
276 return;
277
278 if (type1->code () == TYPE_CODE_DECFLOAT
279 || type2->code () == TYPE_CODE_DECFLOAT)
280 {
281 /* No promotion required. */
282 }
283 else if (type1->code () == TYPE_CODE_FLT
284 || type2->code () == TYPE_CODE_FLT)
285 {
286 switch (language->la_language)
287 {
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;
295
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;
307 }
308 }
309 else if (type1->code () == TYPE_CODE_BOOL
310 && type2->code () == TYPE_CODE_BOOL)
311 {
312 /* No promotion required. */
313 }
314 else
315 /* Integral operations here. */
316 /* FIXME: Also mixed integral/booleans, with result an integer. */
317 {
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;
325
326 /* Determine type length and signedness after promotion for
327 both operands. */
328 if (promoted_len1 < builtin->builtin_int->length ())
329 {
330 is_unsigned1 = 0;
331 promoted_len1 = builtin->builtin_int->length ();
332 }
333 if (promoted_len2 < builtin->builtin_int->length ())
334 {
335 is_unsigned2 = 0;
336 promoted_len2 = builtin->builtin_int->length ();
337 }
338
339 if (promoted_len1 > promoted_len2)
340 {
341 unsigned_operation = is_unsigned1;
342 result_len = promoted_len1;
343 }
344 else if (promoted_len2 > promoted_len1)
345 {
346 unsigned_operation = is_unsigned2;
347 result_len = promoted_len2;
348 }
349 else
350 {
351 unsigned_operation = is_unsigned1 || is_unsigned2;
352 result_len = promoted_len1;
353 }
354
355 switch (language->la_language)
356 {
357 case language_opencl:
358 if (result_len
359 <= lookup_signed_typename (language, "int")->length())
360 {
361 promoted_type =
362 (unsigned_operation
363 ? lookup_unsigned_typename (language, "int")
364 : lookup_signed_typename (language, "int"));
365 }
366 else if (result_len
367 <= lookup_signed_typename (language, "long")->length())
368 {
369 promoted_type =
370 (unsigned_operation
371 ? lookup_unsigned_typename (language, "long")
372 : lookup_signed_typename (language,"long"));
373 }
374 break;
375 default:
376 if (result_len <= builtin->builtin_int->length ())
377 {
378 promoted_type = (unsigned_operation
379 ? builtin->builtin_unsigned_int
380 : builtin->builtin_int);
381 }
382 else if (result_len <= builtin->builtin_long->length ())
383 {
384 promoted_type = (unsigned_operation
385 ? builtin->builtin_unsigned_long
386 : builtin->builtin_long);
387 }
388 else if (result_len <= builtin->builtin_long_long->length ())
389 {
390 promoted_type = (unsigned_operation
391 ? builtin->builtin_unsigned_long_long
392 : builtin->builtin_long_long);
393 }
394 else
395 {
396 promoted_type = (unsigned_operation
397 ? builtin->builtin_uint128
398 : builtin->builtin_int128);
399 }
400 break;
401 }
402 }
403
404 if (promoted_type)
405 {
406 /* Promote both operands to common type. */
407 *arg1 = value_cast (promoted_type, *arg1);
408 *arg2 = value_cast (promoted_type, *arg2);
409 }
410 }
411
412 static int
413 ptrmath_type_p (const struct language_defn *lang, struct type *type)
414 {
415 type = check_typedef (type);
416 if (TYPE_IS_REFERENCE (type))
417 type = type->target_type ();
418
419 switch (type->code ())
420 {
421 case TYPE_CODE_PTR:
422 case TYPE_CODE_FUNC:
423 return 1;
424
425 case TYPE_CODE_ARRAY:
426 return type->is_vector () ? 0 : lang->c_style_arrays_p ();
427
428 default:
429 return 0;
430 }
431 }
432
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. */
438
439 class fake_method
440 {
441 public:
442 fake_method (type_instance_flags flags,
443 int num_types, struct type **param_types);
444 ~fake_method ();
445
446 /* The constructed type. */
447 struct type *type () { return &m_type; }
448
449 private:
450 struct type m_type {};
451 main_type m_main_type {};
452 };
453
454 fake_method::fake_method (type_instance_flags flags,
455 int num_types, struct type **param_types)
456 {
457 struct type *type = &m_type;
458
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)
465 {
466 if (param_types[num_types - 1] == NULL)
467 {
468 --num_types;
469 type->set_has_varargs (true);
470 }
471 else if (check_typedef (param_types[num_types - 1])->code ()
472 == TYPE_CODE_VOID)
473 {
474 --num_types;
475 /* Caller should have ensured this. */
476 gdb_assert (num_types == 0);
477 type->set_is_prototyped (true);
478 }
479 }
480
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));
488
489 while (num_types-- > 0)
490 type->field (num_types).set_type (param_types[num_types]);
491 }
492
493 fake_method::~fake_method ()
494 {
495 xfree (m_type.fields ());
496 }
497
498 namespace expr
499 {
500
501 value *
502 type_instance_operation::evaluate (struct type *expect_type,
503 struct expression *exp,
504 enum noside noside)
505 {
506 type_instance_flags flags = std::get<0> (m_storage);
507 std::vector<type *> &types = std::get<1> (m_storage);
508
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);
512 }
513
514 }
515
516 /* Helper for evaluating an OP_VAR_VALUE. */
517
518 value *
519 evaluate_var_value (enum noside noside, const block *blk, symbol *var)
520 {
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. */
529
530 struct value *ret = NULL;
531
532 try
533 {
534 ret = value_of_variable (var, blk);
535 }
536
537 catch (const gdb_exception_error &except)
538 {
539 if (noside != EVAL_AVOID_SIDE_EFFECTS)
540 throw;
541
542 ret = value::zero (var->type (), not_lval);
543 }
544
545 return ret;
546 }
547
548 namespace expr
549
550 {
551
552 value *
553 var_value_operation::evaluate (struct type *expect_type,
554 struct expression *exp,
555 enum noside noside)
556 {
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);
561 }
562
563 } /* namespace expr */
564
565 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
566
567 value *
568 evaluate_var_msym_value (enum noside noside,
569 struct objfile *objfile, minimal_symbol *msymbol)
570 {
571 CORE_ADDR address;
572 type *the_type = find_minsym_type_and_address (msymbol, objfile, &address);
573
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);
578 }
579
580 /* See expression.h. */
581
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)
588 {
589 if (callee == NULL)
590 error (_("Cannot evaluate function -- may be inlined"));
591
592 type *ftype = callee->type ();
593
594 /* If the callee is a struct, there might be a user-defined function call
595 operator that should be used instead. */
596 std::vector<value *> vals;
597 if (overload_resolution
598 && exp->language_defn->la_language == language_cplus
599 && check_typedef (ftype)->code () == TYPE_CODE_STRUCT)
600 {
601 /* Include space for the `this' pointer at the start. */
602 vals.resize (argvec.size () + 1);
603
604 vals[0] = value_addr (callee);
605 for (int i = 0; i < argvec.size (); ++i)
606 vals[i + 1] = argvec[i];
607
608 int static_memfuncp;
609 find_overload_match (vals, "operator()", METHOD, &vals[0], nullptr,
610 &callee, nullptr, &static_memfuncp, 0, noside);
611 if (!static_memfuncp)
612 argvec = vals;
613
614 ftype = callee->type ();
615 }
616
617 if (noside == EVAL_AVOID_SIDE_EFFECTS)
618 {
619 /* If the return type doesn't look like a function type,
620 call an error. This can happen if somebody tries to turn
621 a variable into a function call. */
622
623 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
624 {
625 /* The call to call_internal_function below handles noside. */
626 }
627 else if (ftype->code () == TYPE_CODE_XMETHOD)
628 {
629 type *return_type = callee->result_type_of_xmethod (argvec);
630
631 if (return_type == NULL)
632 error (_("Xmethod is missing return type."));
633 return value::zero (return_type, not_lval);
634 }
635 else if (ftype->code () == TYPE_CODE_FUNC
636 || ftype->code () == TYPE_CODE_METHOD)
637 {
638 if (ftype->is_gnu_ifunc ())
639 {
640 CORE_ADDR address = callee->address ();
641 type *resolved_type = find_gnu_ifunc_target_type (address);
642
643 if (resolved_type != NULL)
644 ftype = resolved_type;
645 }
646
647 type *return_type = ftype->target_type ();
648
649 if (return_type == NULL)
650 return_type = default_return_type;
651
652 if (return_type == NULL)
653 error_call_unknown_return_type (function_name);
654
655 return value::allocate (return_type);
656 }
657 else
658 error (_("Expression of type other than "
659 "\"Function returning ...\" used as function"));
660 }
661 switch (callee->type ()->code ())
662 {
663 case TYPE_CODE_INTERNAL_FUNCTION:
664 return call_internal_function (exp->gdbarch, exp->language_defn,
665 callee, argvec.size (), argvec.data (),
666 noside);
667 case TYPE_CODE_XMETHOD:
668 return callee->call_xmethod (argvec);
669 default:
670 return call_function_by_hand (callee, default_return_type, argvec);
671 }
672 }
673
674 namespace expr
675 {
676
677 value *
678 operation::evaluate_funcall (struct type *expect_type,
679 struct expression *exp,
680 enum noside noside,
681 const char *function_name,
682 const std::vector<operation_up> &args)
683 {
684 std::vector<value *> vals (args.size ());
685
686 value *callee = evaluate_with_coercion (exp, noside);
687 struct type *type = callee->type ();
688 if (type->code () == TYPE_CODE_PTR)
689 type = type->target_type ();
690 /* If type is a struct, num_fields would refer to the number of
691 members in the type, not the number of arguments. */
692 bool type_has_arguments
693 = type->code () == TYPE_CODE_FUNC || type->code () == TYPE_CODE_METHOD;
694 for (int i = 0; i < args.size (); ++i)
695 {
696 if (type_has_arguments && i < type->num_fields ())
697 vals[i] = args[i]->evaluate (type->field (i).type (), exp, noside);
698 else
699 vals[i] = args[i]->evaluate_with_coercion (exp, noside);
700 }
701
702 return evaluate_subexp_do_call (exp, noside, callee, vals,
703 function_name, expect_type);
704 }
705
706 value *
707 var_value_operation::evaluate_funcall (struct type *expect_type,
708 struct expression *exp,
709 enum noside noside,
710 const std::vector<operation_up> &args)
711 {
712 if (!overload_resolution
713 || exp->language_defn->la_language != language_cplus)
714 return operation::evaluate_funcall (expect_type, exp, noside, args);
715
716 std::vector<value *> argvec (args.size ());
717 for (int i = 0; i < args.size (); ++i)
718 argvec[i] = args[i]->evaluate_with_coercion (exp, noside);
719
720 struct symbol *symp;
721 find_overload_match (argvec, NULL, NON_METHOD,
722 NULL, std::get<0> (m_storage).symbol,
723 NULL, &symp, NULL, 0, noside);
724
725 if (symp->type ()->code () == TYPE_CODE_ERROR)
726 error_unknown_type (symp->print_name ());
727 value *callee = evaluate_var_value (noside, std::get<0> (m_storage).block,
728 symp);
729
730 return evaluate_subexp_do_call (exp, noside, callee, argvec,
731 nullptr, expect_type);
732 }
733
734 value *
735 scope_operation::evaluate_funcall (struct type *expect_type,
736 struct expression *exp,
737 enum noside noside,
738 const std::vector<operation_up> &args)
739 {
740 if (!overload_resolution
741 || exp->language_defn->la_language != language_cplus)
742 return operation::evaluate_funcall (expect_type, exp, noside, args);
743
744 /* Unpack it locally so we can properly handle overload
745 resolution. */
746 const std::string &name = std::get<1> (m_storage);
747 struct type *type = std::get<0> (m_storage);
748
749 symbol *function = NULL;
750 const char *function_name = NULL;
751 std::vector<value *> argvec (1 + args.size ());
752 if (type->code () == TYPE_CODE_NAMESPACE)
753 {
754 function = cp_lookup_symbol_namespace (type->name (),
755 name.c_str (),
756 get_selected_block (0),
757 SEARCH_FUNCTION_DOMAIN).symbol;
758 if (function == NULL)
759 error (_("No symbol \"%s\" in namespace \"%s\"."),
760 name.c_str (), type->name ());
761 }
762 else
763 {
764 gdb_assert (type->code () == TYPE_CODE_STRUCT
765 || type->code () == TYPE_CODE_UNION);
766 function_name = name.c_str ();
767
768 /* We need a properly typed value for method lookup. */
769 argvec[0] = value::zero (type, lval_memory);
770 }
771
772 for (int i = 0; i < args.size (); ++i)
773 argvec[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
774 gdb::array_view<value *> arg_view = argvec;
775
776 value *callee = nullptr;
777 if (function_name != nullptr)
778 {
779 int static_memfuncp;
780
781 find_overload_match (arg_view, function_name, METHOD,
782 &argvec[0], nullptr, &callee, nullptr,
783 &static_memfuncp, 0, noside);
784 if (!static_memfuncp)
785 {
786 /* For the time being, we don't handle this. */
787 error (_("Call to overloaded function %s requires "
788 "`this' pointer"),
789 function_name);
790 }
791
792 arg_view = arg_view.slice (1);
793 }
794 else
795 {
796 symbol *symp;
797 arg_view = arg_view.slice (1);
798 find_overload_match (arg_view, nullptr,
799 NON_METHOD, nullptr, function,
800 nullptr, &symp, nullptr, 1, noside);
801 callee = value_of_variable (symp, get_selected_block (0));
802 }
803
804 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
805 nullptr, expect_type);
806 }
807
808 value *
809 structop_member_base::evaluate_funcall (struct type *expect_type,
810 struct expression *exp,
811 enum noside noside,
812 const std::vector<operation_up> &args)
813 {
814 /* First, evaluate the structure into lhs. */
815 value *lhs;
816 if (opcode () == STRUCTOP_MEMBER)
817 lhs = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
818 else
819 lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
820
821 std::vector<value *> vals (args.size () + 1);
822 gdb::array_view<value *> val_view = vals;
823 /* If the function is a virtual function, then the aggregate
824 value (providing the structure) plays its part by providing
825 the vtable. Otherwise, it is just along for the ride: call
826 the function directly. */
827 value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
828 value *callee;
829
830 type *a1_type = check_typedef (rhs->type ());
831 if (a1_type->code () == TYPE_CODE_METHODPTR)
832 {
833 if (noside == EVAL_AVOID_SIDE_EFFECTS)
834 callee = value::zero (a1_type->target_type (), not_lval);
835 else
836 callee = cplus_method_ptr_to_value (&lhs, rhs);
837
838 vals[0] = lhs;
839 }
840 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
841 {
842 struct type *type_ptr
843 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
844 struct type *target_type_ptr
845 = lookup_pointer_type (a1_type->target_type ());
846
847 /* Now, convert this value to an address. */
848 lhs = value_cast (type_ptr, lhs);
849
850 long mem_offset = value_as_long (rhs);
851
852 callee = value_from_pointer (target_type_ptr,
853 value_as_long (lhs) + mem_offset);
854 callee = value_ind (callee);
855
856 val_view = val_view.slice (1);
857 }
858 else
859 error (_("Non-pointer-to-member value used in pointer-to-member "
860 "construct"));
861
862 for (int i = 0; i < args.size (); ++i)
863 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
864
865 return evaluate_subexp_do_call (exp, noside, callee, val_view,
866 nullptr, expect_type);
867
868 }
869
870 value *
871 structop_base_operation::evaluate_funcall
872 (struct type *expect_type, struct expression *exp, enum noside noside,
873 const std::vector<operation_up> &args)
874 {
875 /* Allocate space for the function call arguments, Including space for a
876 `this' pointer at the start. */
877 std::vector<value *> vals (args.size () + 1);
878 /* First, evaluate the structure into vals[0]. */
879 enum exp_opcode op = opcode ();
880 if (op == STRUCTOP_STRUCT)
881 {
882 /* If v is a variable in a register, and the user types
883 v.method (), this will produce an error, because v has no
884 address.
885
886 A possible way around this would be to allocate a copy of
887 the variable on the stack, copy in the contents, call the
888 function, and copy out the contents. I.e. convert this
889 from call by reference to call by copy-return (or
890 whatever it's called). However, this does not work
891 because it is not the same: the method being called could
892 stash a copy of the address, and then future uses through
893 that address (after the method returns) would be expected
894 to use the variable itself, not some copy of it. */
895 vals[0] = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
896 }
897 else
898 {
899 vals[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
900 /* Check to see if the operator '->' has been overloaded.
901 If the operator has been overloaded replace vals[0] with the
902 value returned by the custom operator and continue
903 evaluation. */
904 while (unop_user_defined_p (op, vals[0]))
905 {
906 struct value *value = nullptr;
907 try
908 {
909 value = value_x_unop (vals[0], op, noside);
910 }
911 catch (const gdb_exception_error &except)
912 {
913 if (except.error == NOT_FOUND_ERROR)
914 break;
915 else
916 throw;
917 }
918
919 vals[0] = value;
920 }
921 }
922
923 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
924 pointer we placed into vals[0]. */
925 for (int i = 0; i < args.size (); ++i)
926 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
927
928 /* The array view includes the `this' pointer. */
929 gdb::array_view<value *> arg_view (vals);
930
931 int static_memfuncp;
932 value *callee;
933 const char *tstr = std::get<1> (m_storage).c_str ();
934 if (overload_resolution
935 && exp->language_defn->la_language == language_cplus)
936 {
937 /* Language is C++, do some overload resolution before
938 evaluation. */
939 value *val0 = vals[0];
940 find_overload_match (arg_view, tstr, METHOD,
941 &val0, nullptr, &callee, nullptr,
942 &static_memfuncp, 0, noside);
943 vals[0] = val0;
944 }
945 else
946 /* Non-C++ case -- or no overload resolution. */
947 {
948 struct value *temp = vals[0];
949
950 callee = value_struct_elt (&temp, arg_view, tstr,
951 &static_memfuncp,
952 op == STRUCTOP_STRUCT
953 ? "structure" : "structure pointer");
954 /* value_struct_elt updates temp with the correct value of the
955 ``this'' pointer if necessary, so modify it to reflect any
956 ``this'' changes. */
957 vals[0] = value_from_longest (lookup_pointer_type (temp->type ()),
958 temp->address ()
959 + temp->embedded_offset ());
960 }
961
962 /* Take out `this' if needed. */
963 if (static_memfuncp)
964 arg_view = arg_view.slice (1);
965
966 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
967 nullptr, expect_type);
968 }
969
970 /* Helper for structop_base_operation::complete which recursively adds
971 field and method names from TYPE, a struct or union type, to the
972 OUTPUT list. PREFIX is prepended to each result. */
973
974 static void
975 add_struct_fields (struct type *type, completion_list &output,
976 const char *fieldname, int namelen, const char *prefix)
977 {
978 int i;
979 int computed_type_name = 0;
980 const char *type_name = NULL;
981
982 type = check_typedef (type);
983 for (i = 0; i < type->num_fields (); ++i)
984 {
985 if (i < TYPE_N_BASECLASSES (type))
986 add_struct_fields (TYPE_BASECLASS (type, i),
987 output, fieldname, namelen, prefix);
988 else if (type->field (i).name ())
989 {
990 if (type->field (i).name ()[0] != '\0')
991 {
992 if (! strncmp (type->field (i).name (),
993 fieldname, namelen))
994 output.emplace_back (concat (prefix, type->field (i).name (),
995 nullptr));
996 }
997 else if (type->field (i).type ()->code () == TYPE_CODE_UNION
998 || type->field (i).type ()->code () == TYPE_CODE_STRUCT)
999 {
1000 /* Recurse into anonymous unions and structures. */
1001 add_struct_fields (type->field (i).type (),
1002 output, fieldname, namelen, prefix);
1003 }
1004 }
1005 }
1006
1007 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1008 {
1009 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
1010
1011 if (name && ! strncmp (name, fieldname, namelen))
1012 {
1013 if (!computed_type_name)
1014 {
1015 type_name = type->name ();
1016 computed_type_name = 1;
1017 }
1018 /* Omit constructors from the completion list. */
1019 if (!type_name || strcmp (type_name, name))
1020 output.emplace_back (concat (prefix, name, nullptr));
1021 }
1022 }
1023 }
1024
1025 /* See expop.h. */
1026
1027 bool
1028 structop_base_operation::complete (struct expression *exp,
1029 completion_tracker &tracker,
1030 const char *prefix)
1031 {
1032 const std::string &fieldname = std::get<1> (m_storage);
1033
1034 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp,
1035 EVAL_AVOID_SIDE_EFFECTS);
1036 struct type *type = lhs->type ();
1037 for (;;)
1038 {
1039 type = check_typedef (type);
1040 if (!type->is_pointer_or_reference ())
1041 break;
1042 type = type->target_type ();
1043 }
1044
1045 if (type->code () == TYPE_CODE_UNION
1046 || type->code () == TYPE_CODE_STRUCT)
1047 {
1048 completion_list result;
1049
1050 add_struct_fields (type, result, fieldname.c_str (),
1051 fieldname.length (), prefix);
1052 tracker.add_completions (std::move (result));
1053 return true;
1054 }
1055
1056 return false;
1057 }
1058
1059 } /* namespace expr */
1060
1061 /* Return true if type is integral or reference to integral */
1062
1063 static bool
1064 is_integral_or_integral_reference (struct type *type)
1065 {
1066 if (is_integral_type (type))
1067 return true;
1068
1069 type = check_typedef (type);
1070 return (type != nullptr
1071 && TYPE_IS_REFERENCE (type)
1072 && is_integral_type (type->target_type ()));
1073 }
1074
1075 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1076
1077 struct value *
1078 eval_op_var_entry_value (struct type *expect_type, struct expression *exp,
1079 enum noside noside, symbol *sym)
1080 {
1081 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1082 return value::zero (sym->type (), not_lval);
1083
1084 const symbol_computed_ops *computed_ops = sym->computed_ops ();
1085 if (computed_ops == nullptr
1086 || computed_ops->read_variable_at_entry == nullptr)
1087 error (_("Symbol \"%s\" does not have any specific entry value"),
1088 sym->print_name ());
1089
1090 frame_info_ptr frame = get_selected_frame (NULL);
1091 return computed_ops->read_variable_at_entry (sym, frame);
1092 }
1093
1094 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1095
1096 struct value *
1097 eval_op_var_msym_value (struct type *expect_type, struct expression *exp,
1098 enum noside noside, bool outermost_p,
1099 bound_minimal_symbol msymbol)
1100 {
1101 value *val = evaluate_var_msym_value (noside, msymbol.objfile,
1102 msymbol.minsym);
1103
1104 struct type *type = val->type ();
1105 if (type->code () == TYPE_CODE_ERROR
1106 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p))
1107 error_unknown_type (msymbol.minsym->print_name ());
1108 return val;
1109 }
1110
1111 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1112
1113 struct value *
1114 eval_op_func_static_var (struct type *expect_type, struct expression *exp,
1115 enum noside noside,
1116 value *func, const char *var)
1117 {
1118 CORE_ADDR addr = func->address ();
1119 const block *blk = block_for_pc (addr);
1120 struct block_symbol sym = lookup_symbol (var, blk, SEARCH_VAR_DOMAIN,
1121 nullptr);
1122 if (sym.symbol == NULL)
1123 error (_("No symbol \"%s\" in specified context."), var);
1124 return evaluate_var_value (noside, sym.block, sym.symbol);
1125 }
1126
1127 /* Helper function that implements the body of OP_REGISTER. */
1128
1129 struct value *
1130 eval_op_register (struct type *expect_type, struct expression *exp,
1131 enum noside noside, const char *name)
1132 {
1133 int regno;
1134 struct value *val;
1135
1136 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1137 name, strlen (name));
1138 if (regno == -1)
1139 error (_("Register $%s not available."), name);
1140
1141 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1142 a value with the appropriate register type. Unfortunately,
1143 we don't have easy access to the type of user registers.
1144 So for these registers, we fetch the register value regardless
1145 of the evaluation mode. */
1146 if (noside == EVAL_AVOID_SIDE_EFFECTS
1147 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
1148 val = value::zero (register_type (exp->gdbarch, regno), not_lval);
1149 else
1150 val = value_of_register
1151 (regno, get_next_frame_sentinel_okay (get_selected_frame ()));
1152 if (val == NULL)
1153 error (_("Value of register %s not available."), name);
1154 else
1155 return val;
1156 }
1157
1158 namespace expr
1159 {
1160
1161 value *
1162 string_operation::evaluate (struct type *expect_type,
1163 struct expression *exp,
1164 enum noside noside)
1165 {
1166 const std::string &str = std::get<0> (m_storage);
1167 struct type *type = language_string_char_type (exp->language_defn,
1168 exp->gdbarch);
1169 return value_string (str.c_str (), str.size (), type);
1170 }
1171
1172 struct value *
1173 ternop_slice_operation::evaluate (struct type *expect_type,
1174 struct expression *exp,
1175 enum noside noside)
1176 {
1177 struct value *array
1178 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1179 struct value *low
1180 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1181 struct value *upper
1182 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
1183
1184 int lowbound = value_as_long (low);
1185 int upperbound = value_as_long (upper);
1186 return value_slice (array, lowbound, upperbound - lowbound + 1);
1187 }
1188
1189 } /* namespace expr */
1190
1191 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1192
1193 struct value *
1194 eval_op_objc_selector (struct type *expect_type, struct expression *exp,
1195 enum noside noside,
1196 const char *sel)
1197 {
1198 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1199 return value_from_longest (selector_type,
1200 lookup_child_selector (exp->gdbarch, sel));
1201 }
1202
1203 /* A helper function for STRUCTOP_STRUCT. */
1204
1205 struct value *
1206 eval_op_structop_struct (struct type *expect_type, struct expression *exp,
1207 enum noside noside,
1208 struct value *arg1, const char *string)
1209 {
1210 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1211 NULL, "structure");
1212 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1213 arg3 = value::zero (arg3->type (), arg3->lval ());
1214 return arg3;
1215 }
1216
1217 /* A helper function for STRUCTOP_PTR. */
1218
1219 struct value *
1220 eval_op_structop_ptr (struct type *expect_type, struct expression *exp,
1221 enum noside noside,
1222 struct value *arg1, const char *string)
1223 {
1224 /* Check to see if operator '->' has been overloaded. If so replace
1225 arg1 with the value returned by evaluating operator->(). */
1226 while (unop_user_defined_p (STRUCTOP_PTR, arg1))
1227 {
1228 struct value *value = NULL;
1229 try
1230 {
1231 value = value_x_unop (arg1, STRUCTOP_PTR, noside);
1232 }
1233
1234 catch (const gdb_exception_error &except)
1235 {
1236 if (except.error == NOT_FOUND_ERROR)
1237 break;
1238 else
1239 throw;
1240 }
1241
1242 arg1 = value;
1243 }
1244
1245 /* JYG: if print object is on we need to replace the base type
1246 with rtti type in order to continue on with successful
1247 lookup of member / method only available in the rtti type. */
1248 {
1249 struct type *arg_type = arg1->type ();
1250 struct type *real_type;
1251 int full, using_enc;
1252 LONGEST top;
1253 struct value_print_options opts;
1254
1255 get_user_print_options (&opts);
1256 if (opts.objectprint && arg_type->target_type ()
1257 && (arg_type->target_type ()->code () == TYPE_CODE_STRUCT))
1258 {
1259 real_type = value_rtti_indirect_type (arg1, &full, &top,
1260 &using_enc);
1261 if (real_type)
1262 arg1 = value_cast (real_type, arg1);
1263 }
1264 }
1265
1266 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1267 NULL, "structure pointer");
1268 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1269 arg3 = value::zero (arg3->type (), arg3->lval ());
1270 return arg3;
1271 }
1272
1273 /* A helper function for STRUCTOP_MEMBER. */
1274
1275 struct value *
1276 eval_op_member (struct type *expect_type, struct expression *exp,
1277 enum noside noside,
1278 struct value *arg1, struct value *arg2)
1279 {
1280 long mem_offset;
1281
1282 struct value *arg3;
1283 struct type *type = check_typedef (arg2->type ());
1284 switch (type->code ())
1285 {
1286 case TYPE_CODE_METHODPTR:
1287 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1288 return value::zero (type->target_type (), not_lval);
1289 else
1290 {
1291 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1292 gdb_assert (arg2->type ()->code () == TYPE_CODE_PTR);
1293 return value_ind (arg2);
1294 }
1295
1296 case TYPE_CODE_MEMBERPTR:
1297 /* Now, convert these values to an address. */
1298 if (check_typedef (arg1->type ())->code () != TYPE_CODE_PTR)
1299 arg1 = value_addr (arg1);
1300 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1301 arg1, 1);
1302
1303 mem_offset = value_as_long (arg2);
1304
1305 arg3 = value_from_pointer (lookup_pointer_type (type->target_type ()),
1306 value_as_long (arg1) + mem_offset);
1307 return value_ind (arg3);
1308
1309 default:
1310 error (_("non-pointer-to-member value used "
1311 "in pointer-to-member construct"));
1312 }
1313 }
1314
1315 /* A helper function for BINOP_ADD. */
1316
1317 struct value *
1318 eval_op_add (struct type *expect_type, struct expression *exp,
1319 enum noside noside,
1320 struct value *arg1, struct value *arg2)
1321 {
1322 if (binop_user_defined_p (BINOP_ADD, arg1, arg2))
1323 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside);
1324 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1325 && is_integral_or_integral_reference (arg2->type ()))
1326 return value_ptradd (arg1, value_as_long (arg2));
1327 else if (ptrmath_type_p (exp->language_defn, arg2->type ())
1328 && is_integral_or_integral_reference (arg1->type ()))
1329 return value_ptradd (arg2, value_as_long (arg1));
1330 else
1331 {
1332 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1333 return value_binop (arg1, arg2, BINOP_ADD);
1334 }
1335 }
1336
1337 /* A helper function for BINOP_SUB. */
1338
1339 struct value *
1340 eval_op_sub (struct type *expect_type, struct expression *exp,
1341 enum noside noside,
1342 struct value *arg1, struct value *arg2)
1343 {
1344 if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
1345 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
1346 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1347 && ptrmath_type_p (exp->language_defn, arg2->type ()))
1348 {
1349 /* FIXME -- should be ptrdiff_t */
1350 struct type *type = builtin_type (exp->gdbarch)->builtin_long;
1351 return value_from_longest (type, value_ptrdiff (arg1, arg2));
1352 }
1353 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1354 && is_integral_or_integral_reference (arg2->type ()))
1355 return value_ptradd (arg1, - value_as_long (arg2));
1356 else
1357 {
1358 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1359 return value_binop (arg1, arg2, BINOP_SUB);
1360 }
1361 }
1362
1363 /* Helper function for several different binary operations. */
1364
1365 struct value *
1366 eval_op_binary (struct type *expect_type, struct expression *exp,
1367 enum noside noside, enum exp_opcode op,
1368 struct value *arg1, struct value *arg2)
1369 {
1370 if (binop_user_defined_p (op, arg1, arg2))
1371 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1372 else
1373 {
1374 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1375 fudge arg2 to avoid division-by-zero, the caller is
1376 (theoretically) only looking for the type of the result. */
1377 if (noside == EVAL_AVOID_SIDE_EFFECTS
1378 /* ??? Do we really want to test for BINOP_MOD here?
1379 The implementation of value_binop gives it a well-defined
1380 value. */
1381 && (op == BINOP_DIV
1382 || op == BINOP_INTDIV
1383 || op == BINOP_REM
1384 || op == BINOP_MOD)
1385 && value_logical_not (arg2))
1386 {
1387 struct value *v_one;
1388
1389 v_one = value_one (arg2->type ());
1390 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
1391 return value_binop (arg1, v_one, op);
1392 }
1393 else
1394 {
1395 /* For shift and integer exponentiation operations,
1396 only promote the first argument. */
1397 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1398 && is_integral_type (arg2->type ()))
1399 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1400 else
1401 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1402
1403 return value_binop (arg1, arg2, op);
1404 }
1405 }
1406 }
1407
1408 /* A helper function for BINOP_SUBSCRIPT. */
1409
1410 struct value *
1411 eval_op_subscript (struct type *expect_type, struct expression *exp,
1412 enum noside noside, enum exp_opcode op,
1413 struct value *arg1, struct value *arg2)
1414 {
1415 if (binop_user_defined_p (op, arg1, arg2))
1416 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1417 else
1418 {
1419 /* If the user attempts to subscript something that is not an
1420 array or pointer type (like a plain int variable for example),
1421 then report this as an error. */
1422
1423 arg1 = coerce_ref (arg1);
1424 struct type *type = check_typedef (arg1->type ());
1425 if (type->code () != TYPE_CODE_ARRAY
1426 && type->code () != TYPE_CODE_PTR)
1427 {
1428 if (type->name ())
1429 error (_("cannot subscript something of type `%s'"),
1430 type->name ());
1431 else
1432 error (_("cannot subscript requested type"));
1433 }
1434
1435 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1436 return value::zero (type->target_type (), arg1->lval ());
1437 else
1438 return value_subscript (arg1, value_as_long (arg2));
1439 }
1440 }
1441
1442 /* A helper function for BINOP_EQUAL. */
1443
1444 struct value *
1445 eval_op_equal (struct type *expect_type, struct expression *exp,
1446 enum noside noside, enum exp_opcode op,
1447 struct value *arg1, struct value *arg2)
1448 {
1449 if (binop_user_defined_p (op, arg1, arg2))
1450 {
1451 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1452 }
1453 else
1454 {
1455 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1456 int tem = value_equal (arg1, arg2);
1457 struct type *type = language_bool_type (exp->language_defn,
1458 exp->gdbarch);
1459 return value_from_longest (type, (LONGEST) tem);
1460 }
1461 }
1462
1463 /* A helper function for BINOP_NOTEQUAL. */
1464
1465 struct value *
1466 eval_op_notequal (struct type *expect_type, struct expression *exp,
1467 enum noside noside, enum exp_opcode op,
1468 struct value *arg1, struct value *arg2)
1469 {
1470 if (binop_user_defined_p (op, arg1, arg2))
1471 {
1472 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1473 }
1474 else
1475 {
1476 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1477 int tem = value_equal (arg1, arg2);
1478 struct type *type = language_bool_type (exp->language_defn,
1479 exp->gdbarch);
1480 return value_from_longest (type, (LONGEST) ! tem);
1481 }
1482 }
1483
1484 /* A helper function for BINOP_LESS. */
1485
1486 struct value *
1487 eval_op_less (struct type *expect_type, struct expression *exp,
1488 enum noside noside, enum exp_opcode op,
1489 struct value *arg1, struct value *arg2)
1490 {
1491 if (binop_user_defined_p (op, arg1, arg2))
1492 {
1493 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1494 }
1495 else
1496 {
1497 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1498 int tem = value_less (arg1, arg2);
1499 struct type *type = language_bool_type (exp->language_defn,
1500 exp->gdbarch);
1501 return value_from_longest (type, (LONGEST) tem);
1502 }
1503 }
1504
1505 /* A helper function for BINOP_GTR. */
1506
1507 struct value *
1508 eval_op_gtr (struct type *expect_type, struct expression *exp,
1509 enum noside noside, enum exp_opcode op,
1510 struct value *arg1, struct value *arg2)
1511 {
1512 if (binop_user_defined_p (op, arg1, arg2))
1513 {
1514 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1515 }
1516 else
1517 {
1518 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1519 int tem = value_less (arg2, arg1);
1520 struct type *type = language_bool_type (exp->language_defn,
1521 exp->gdbarch);
1522 return value_from_longest (type, (LONGEST) tem);
1523 }
1524 }
1525
1526 /* A helper function for BINOP_GEQ. */
1527
1528 struct value *
1529 eval_op_geq (struct type *expect_type, struct expression *exp,
1530 enum noside noside, enum exp_opcode op,
1531 struct value *arg1, struct value *arg2)
1532 {
1533 if (binop_user_defined_p (op, arg1, arg2))
1534 {
1535 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1536 }
1537 else
1538 {
1539 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1540 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1541 struct type *type = language_bool_type (exp->language_defn,
1542 exp->gdbarch);
1543 return value_from_longest (type, (LONGEST) tem);
1544 }
1545 }
1546
1547 /* A helper function for BINOP_LEQ. */
1548
1549 struct value *
1550 eval_op_leq (struct type *expect_type, struct expression *exp,
1551 enum noside noside, enum exp_opcode op,
1552 struct value *arg1, struct value *arg2)
1553 {
1554 if (binop_user_defined_p (op, arg1, arg2))
1555 {
1556 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1557 }
1558 else
1559 {
1560 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1561 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1562 struct type *type = language_bool_type (exp->language_defn,
1563 exp->gdbarch);
1564 return value_from_longest (type, (LONGEST) tem);
1565 }
1566 }
1567
1568 /* A helper function for BINOP_REPEAT. */
1569
1570 struct value *
1571 eval_op_repeat (struct type *expect_type, struct expression *exp,
1572 enum noside noside, enum exp_opcode op,
1573 struct value *arg1, struct value *arg2)
1574 {
1575 struct type *type = check_typedef (arg2->type ());
1576 if (type->code () != TYPE_CODE_INT
1577 && type->code () != TYPE_CODE_ENUM)
1578 error (_("Non-integral right operand for \"@\" operator."));
1579 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1580 {
1581 return allocate_repeat_value (arg1->type (),
1582 longest_to_int (value_as_long (arg2)));
1583 }
1584 else
1585 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1586 }
1587
1588 /* A helper function for UNOP_PLUS. */
1589
1590 struct value *
1591 eval_op_plus (struct type *expect_type, struct expression *exp,
1592 enum noside noside, enum exp_opcode op,
1593 struct value *arg1)
1594 {
1595 if (unop_user_defined_p (op, arg1))
1596 return value_x_unop (arg1, op, noside);
1597 else
1598 {
1599 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1600 return value_pos (arg1);
1601 }
1602 }
1603
1604 /* A helper function for UNOP_NEG. */
1605
1606 struct value *
1607 eval_op_neg (struct type *expect_type, struct expression *exp,
1608 enum noside noside, enum exp_opcode op,
1609 struct value *arg1)
1610 {
1611 if (unop_user_defined_p (op, arg1))
1612 return value_x_unop (arg1, op, noside);
1613 else
1614 {
1615 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1616 return value_neg (arg1);
1617 }
1618 }
1619
1620 /* A helper function for UNOP_COMPLEMENT. */
1621
1622 struct value *
1623 eval_op_complement (struct type *expect_type, struct expression *exp,
1624 enum noside noside, enum exp_opcode op,
1625 struct value *arg1)
1626 {
1627 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1628 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1629 else
1630 {
1631 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1632 return value_complement (arg1);
1633 }
1634 }
1635
1636 /* A helper function for UNOP_LOGICAL_NOT. */
1637
1638 struct value *
1639 eval_op_lognot (struct type *expect_type, struct expression *exp,
1640 enum noside noside, enum exp_opcode op,
1641 struct value *arg1)
1642 {
1643 if (unop_user_defined_p (op, arg1))
1644 return value_x_unop (arg1, op, noside);
1645 else
1646 {
1647 struct type *type = language_bool_type (exp->language_defn,
1648 exp->gdbarch);
1649 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
1650 }
1651 }
1652
1653 /* A helper function for UNOP_IND. */
1654
1655 struct value *
1656 eval_op_ind (struct type *expect_type, struct expression *exp,
1657 enum noside noside,
1658 struct value *arg1)
1659 {
1660 struct type *type = check_typedef (arg1->type ());
1661 if (type->code () == TYPE_CODE_METHODPTR
1662 || type->code () == TYPE_CODE_MEMBERPTR)
1663 error (_("Attempt to dereference pointer "
1664 "to member without an object"));
1665 if (unop_user_defined_p (UNOP_IND, arg1))
1666 return value_x_unop (arg1, UNOP_IND, noside);
1667 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1668 {
1669 type = check_typedef (arg1->type ());
1670
1671 /* If the type pointed to is dynamic then in order to resolve the
1672 dynamic properties we must actually dereference the pointer.
1673 There is a risk that this dereference will have side-effects
1674 in the inferior, but being able to print accurate type
1675 information seems worth the risk. */
1676 if (!type->is_pointer_or_reference ()
1677 || !is_dynamic_type (type->target_type ()))
1678 {
1679 if (type->is_pointer_or_reference ()
1680 /* In C you can dereference an array to get the 1st elt. */
1681 || type->code () == TYPE_CODE_ARRAY)
1682 return value::zero (type->target_type (),
1683 lval_memory);
1684 else if (type->code () == TYPE_CODE_INT)
1685 /* GDB allows dereferencing an int. */
1686 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
1687 lval_memory);
1688 else
1689 error (_("Attempt to take contents of a non-pointer value."));
1690 }
1691 }
1692
1693 /* Allow * on an integer so we can cast it to whatever we want.
1694 This returns an int, which seems like the most C-like thing to
1695 do. "long long" variables are rare enough that
1696 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1697 if (type->code () == TYPE_CODE_INT)
1698 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
1699 value_as_address (arg1));
1700 return value_ind (arg1);
1701 }
1702
1703 /* A helper function for UNOP_ALIGNOF. */
1704
1705 struct value *
1706 eval_op_alignof (struct type *expect_type, struct expression *exp,
1707 enum noside noside,
1708 struct value *arg1)
1709 {
1710 struct type *type = arg1->type ();
1711 /* FIXME: This should be size_t. */
1712 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
1713 ULONGEST align = type_align (type);
1714 if (align == 0)
1715 error (_("could not determine alignment of type"));
1716 return value_from_longest (size_type, align);
1717 }
1718
1719 /* A helper function for UNOP_MEMVAL. */
1720
1721 struct value *
1722 eval_op_memval (struct type *expect_type, struct expression *exp,
1723 enum noside noside,
1724 struct value *arg1, struct type *type)
1725 {
1726 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1727 return value::zero (type, lval_memory);
1728 else
1729 return value_at_lazy (type, value_as_address (arg1));
1730 }
1731
1732 /* A helper function for UNOP_PREINCREMENT. */
1733
1734 struct value *
1735 eval_op_preinc (struct type *expect_type, struct expression *exp,
1736 enum noside noside, enum exp_opcode op,
1737 struct value *arg1)
1738 {
1739 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1740 return arg1;
1741 else if (unop_user_defined_p (op, arg1))
1742 {
1743 return value_x_unop (arg1, op, noside);
1744 }
1745 else
1746 {
1747 struct value *arg2;
1748 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1749 arg2 = value_ptradd (arg1, 1);
1750 else
1751 {
1752 struct value *tmp = arg1;
1753
1754 arg2 = value_one (arg1->type ());
1755 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1756 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1757 }
1758
1759 return value_assign (arg1, arg2);
1760 }
1761 }
1762
1763 /* A helper function for UNOP_PREDECREMENT. */
1764
1765 struct value *
1766 eval_op_predec (struct type *expect_type, struct expression *exp,
1767 enum noside noside, enum exp_opcode op,
1768 struct value *arg1)
1769 {
1770 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1771 return arg1;
1772 else if (unop_user_defined_p (op, arg1))
1773 {
1774 return value_x_unop (arg1, op, noside);
1775 }
1776 else
1777 {
1778 struct value *arg2;
1779 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1780 arg2 = value_ptradd (arg1, -1);
1781 else
1782 {
1783 struct value *tmp = arg1;
1784
1785 arg2 = value_one (arg1->type ());
1786 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1787 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1788 }
1789
1790 return value_assign (arg1, arg2);
1791 }
1792 }
1793
1794 /* A helper function for UNOP_POSTINCREMENT. */
1795
1796 struct value *
1797 eval_op_postinc (struct type *expect_type, struct expression *exp,
1798 enum noside noside, enum exp_opcode op,
1799 struct value *arg1)
1800 {
1801 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1802 return arg1;
1803 else if (unop_user_defined_p (op, arg1))
1804 {
1805 return value_x_unop (arg1, op, noside);
1806 }
1807 else
1808 {
1809 struct value *arg3 = arg1->non_lval ();
1810 struct value *arg2;
1811
1812 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1813 arg2 = value_ptradd (arg1, 1);
1814 else
1815 {
1816 struct value *tmp = arg1;
1817
1818 arg2 = value_one (arg1->type ());
1819 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1820 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1821 }
1822
1823 value_assign (arg1, arg2);
1824 return arg3;
1825 }
1826 }
1827
1828 /* A helper function for UNOP_POSTDECREMENT. */
1829
1830 struct value *
1831 eval_op_postdec (struct type *expect_type, struct expression *exp,
1832 enum noside noside, enum exp_opcode op,
1833 struct value *arg1)
1834 {
1835 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1836 return arg1;
1837 else if (unop_user_defined_p (op, arg1))
1838 {
1839 return value_x_unop (arg1, op, noside);
1840 }
1841 else
1842 {
1843 struct value *arg3 = arg1->non_lval ();
1844 struct value *arg2;
1845
1846 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1847 arg2 = value_ptradd (arg1, -1);
1848 else
1849 {
1850 struct value *tmp = arg1;
1851
1852 arg2 = value_one (arg1->type ());
1853 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1854 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1855 }
1856
1857 value_assign (arg1, arg2);
1858 return arg3;
1859 }
1860 }
1861
1862 namespace expr
1863 {
1864
1865 struct value *
1866 type_operation::evaluate (struct type *expect_type, struct expression *exp,
1867 enum noside noside)
1868 {
1869 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1870 return value::allocate (std::get<0> (m_storage));
1871 else
1872 error (_("Attempt to use a type name as an expression"));
1873 }
1874
1875 }
1876
1877 /* A helper function for BINOP_ASSIGN_MODIFY. */
1878
1879 struct value *
1880 eval_binop_assign_modify (struct type *expect_type, struct expression *exp,
1881 enum noside noside, enum exp_opcode op,
1882 struct value *arg1, struct value *arg2)
1883 {
1884 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1885 return arg1;
1886 if (binop_user_defined_p (op, arg1, arg2))
1887 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1888 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1889 arg1->type ())
1890 && is_integral_type (arg2->type ()))
1891 arg2 = value_ptradd (arg1, value_as_long (arg2));
1892 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
1893 arg1->type ())
1894 && is_integral_type (arg2->type ()))
1895 arg2 = value_ptradd (arg1, - value_as_long (arg2));
1896 else
1897 {
1898 struct value *tmp = arg1;
1899
1900 /* For shift and integer exponentiation operations,
1901 only promote the first argument. */
1902 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1903 && is_integral_type (arg2->type ()))
1904 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1905 else
1906 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1907
1908 arg2 = value_binop (tmp, arg2, op);
1909 }
1910 return value_assign (arg1, arg2);
1911 }
1912
1913 /* Note that ARGS needs 2 empty slots up front and must end with a
1914 null pointer. */
1915 static struct value *
1916 eval_op_objc_msgcall (struct type *expect_type, struct expression *exp,
1917 enum noside noside, CORE_ADDR selector,
1918 value *target, gdb::array_view<value *> args)
1919 {
1920 CORE_ADDR responds_selector = 0;
1921 CORE_ADDR method_selector = 0;
1922
1923 int struct_return = 0;
1924
1925 struct value *msg_send = NULL;
1926 struct value *msg_send_stret = NULL;
1927 int gnu_runtime = 0;
1928
1929 struct value *method = NULL;
1930 struct value *called_method = NULL;
1931
1932 struct type *selector_type = NULL;
1933 struct type *long_type;
1934 struct type *type;
1935
1936 struct value *ret = NULL;
1937 CORE_ADDR addr = 0;
1938
1939 value *argvec[5];
1940
1941 long_type = builtin_type (exp->gdbarch)->builtin_long;
1942 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1943
1944 if (value_as_long (target) == 0)
1945 return value_from_longest (long_type, 0);
1946
1947 if (lookup_minimal_symbol (current_program_space, "objc_msg_lookup").minsym
1948 != nullptr)
1949 gnu_runtime = 1;
1950
1951 /* Find the method dispatch (Apple runtime) or method lookup
1952 (GNU runtime) function for Objective-C. These will be used
1953 to lookup the symbol information for the method. If we
1954 can't find any symbol information, then we'll use these to
1955 call the method, otherwise we can call the method
1956 directly. The msg_send_stret function is used in the special
1957 case of a method that returns a structure (Apple runtime
1958 only). */
1959 if (gnu_runtime)
1960 {
1961 type = selector_type;
1962
1963 type = lookup_function_type (type);
1964 type = lookup_pointer_type (type);
1965 type = lookup_function_type (type);
1966 type = lookup_pointer_type (type);
1967
1968 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1969 msg_send_stret
1970 = find_function_in_inferior ("objc_msg_lookup", NULL);
1971
1972 msg_send = value_from_pointer (type, value_as_address (msg_send));
1973 msg_send_stret = value_from_pointer (type,
1974 value_as_address (msg_send_stret));
1975 }
1976 else
1977 {
1978 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1979 /* Special dispatcher for methods returning structs. */
1980 msg_send_stret
1981 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1982 }
1983
1984 /* Verify the target object responds to this method. The
1985 standard top-level 'Object' class uses a different name for
1986 the verification method than the non-standard, but more
1987 often used, 'NSObject' class. Make sure we check for both. */
1988
1989 responds_selector
1990 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1991 if (responds_selector == 0)
1992 responds_selector
1993 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1994
1995 if (responds_selector == 0)
1996 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1997
1998 method_selector
1999 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
2000 if (method_selector == 0)
2001 method_selector
2002 = lookup_child_selector (exp->gdbarch, "methodFor:");
2003
2004 if (method_selector == 0)
2005 error (_("no 'methodFor:' or 'methodForSelector:' method"));
2006
2007 /* Call the verification method, to make sure that the target
2008 class implements the desired method. */
2009
2010 argvec[0] = msg_send;
2011 argvec[1] = target;
2012 argvec[2] = value_from_longest (long_type, responds_selector);
2013 argvec[3] = value_from_longest (long_type, selector);
2014 argvec[4] = 0;
2015
2016 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2017 if (gnu_runtime)
2018 {
2019 /* Function objc_msg_lookup returns a pointer. */
2020 argvec[0] = ret;
2021 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2022 }
2023 if (value_as_long (ret) == 0)
2024 error (_("Target does not respond to this message selector."));
2025
2026 /* Call "methodForSelector:" method, to get the address of a
2027 function method that implements this selector for this
2028 class. If we can find a symbol at that address, then we
2029 know the return type, parameter types etc. (that's a good
2030 thing). */
2031
2032 argvec[0] = msg_send;
2033 argvec[1] = target;
2034 argvec[2] = value_from_longest (long_type, method_selector);
2035 argvec[3] = value_from_longest (long_type, selector);
2036 argvec[4] = 0;
2037
2038 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2039 if (gnu_runtime)
2040 {
2041 argvec[0] = ret;
2042 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2043 }
2044
2045 /* ret should now be the selector. */
2046
2047 addr = value_as_long (ret);
2048 if (addr)
2049 {
2050 struct symbol *sym = NULL;
2051
2052 /* The address might point to a function descriptor;
2053 resolve it to the actual code address instead. */
2054 addr = gdbarch_convert_from_func_ptr_addr
2055 (exp->gdbarch, addr, current_inferior ()->top_target ());
2056
2057 /* Is it a high_level symbol? */
2058 sym = find_pc_function (addr);
2059 if (sym != NULL)
2060 method = value_of_variable (sym, 0);
2061 }
2062
2063 /* If we found a method with symbol information, check to see
2064 if it returns a struct. Otherwise assume it doesn't. */
2065
2066 if (method)
2067 {
2068 CORE_ADDR funaddr;
2069 struct type *val_type;
2070
2071 funaddr = find_function_addr (method, &val_type);
2072
2073 block_for_pc (funaddr);
2074
2075 val_type = check_typedef (val_type);
2076
2077 if ((val_type == NULL)
2078 || (val_type->code () == TYPE_CODE_ERROR))
2079 {
2080 if (expect_type != NULL)
2081 val_type = expect_type;
2082 }
2083
2084 struct_return = using_struct_return (exp->gdbarch, method,
2085 val_type);
2086 }
2087 else if (expect_type != NULL)
2088 {
2089 struct_return = using_struct_return (exp->gdbarch, NULL,
2090 check_typedef (expect_type));
2091 }
2092
2093 /* Found a function symbol. Now we will substitute its
2094 value in place of the message dispatcher (obj_msgSend),
2095 so that we call the method directly instead of through
2096 the dispatcher. The main reason for doing this is that
2097 we can now evaluate the return value and parameter values
2098 according to their known data types, in case we need to
2099 do things like promotion, dereferencing, special handling
2100 of structs and doubles, etc.
2101
2102 We want to use the type signature of 'method', but still
2103 jump to objc_msgSend() or objc_msgSend_stret() to better
2104 mimic the behavior of the runtime. */
2105
2106 if (method)
2107 {
2108 if (method->type ()->code () != TYPE_CODE_FUNC)
2109 error (_("method address has symbol information "
2110 "with non-function type; skipping"));
2111
2112 /* Create a function pointer of the appropriate type, and
2113 replace its value with the value of msg_send or
2114 msg_send_stret. We must use a pointer here, as
2115 msg_send and msg_send_stret are of pointer type, and
2116 the representation may be different on systems that use
2117 function descriptors. */
2118 if (struct_return)
2119 called_method
2120 = value_from_pointer (lookup_pointer_type (method->type ()),
2121 value_as_address (msg_send_stret));
2122 else
2123 called_method
2124 = value_from_pointer (lookup_pointer_type (method->type ()),
2125 value_as_address (msg_send));
2126 }
2127 else
2128 {
2129 if (struct_return)
2130 called_method = msg_send_stret;
2131 else
2132 called_method = msg_send;
2133 }
2134
2135
2136 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2137 {
2138 /* If the return type doesn't look like a function type,
2139 call an error. This can happen if somebody tries to
2140 turn a variable into a function call. This is here
2141 because people often want to call, eg, strcmp, which
2142 gdb doesn't know is a function. If gdb isn't asked for
2143 it's opinion (ie. through "whatis"), it won't offer
2144 it. */
2145
2146 struct type *callee_type = called_method->type ();
2147
2148 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
2149 callee_type = callee_type->target_type ();
2150 callee_type = callee_type->target_type ();
2151
2152 if (callee_type)
2153 {
2154 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
2155 return value::allocate (expect_type);
2156 else
2157 return value::allocate (callee_type);
2158 }
2159 else
2160 error (_("Expression of type other than "
2161 "\"method returning ...\" used as a method"));
2162 }
2163
2164 /* Now depending on whether we found a symbol for the method,
2165 we will either call the runtime dispatcher or the method
2166 directly. */
2167
2168 args[0] = target;
2169 args[1] = value_from_longest (long_type, selector);
2170
2171 if (gnu_runtime && (method != NULL))
2172 {
2173 /* Function objc_msg_lookup returns a pointer. */
2174 struct type *tem_type = called_method->type ();
2175 tem_type = lookup_pointer_type (lookup_function_type (tem_type));
2176 called_method->deprecated_set_type (tem_type);
2177 called_method = call_function_by_hand (called_method, NULL, args);
2178 }
2179
2180 return call_function_by_hand (called_method, NULL, args);
2181 }
2182
2183 /* Helper function for MULTI_SUBSCRIPT. */
2184
2185 static struct value *
2186 eval_multi_subscript (struct type *expect_type, struct expression *exp,
2187 enum noside noside, value *arg1,
2188 gdb::array_view<value *> args)
2189 {
2190 for (value *arg2 : args)
2191 {
2192 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2))
2193 {
2194 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside);
2195 }
2196 else
2197 {
2198 arg1 = coerce_ref (arg1);
2199 struct type *type = check_typedef (arg1->type ());
2200
2201 switch (type->code ())
2202 {
2203 case TYPE_CODE_PTR:
2204 case TYPE_CODE_ARRAY:
2205 case TYPE_CODE_STRING:
2206 arg1 = value_subscript (arg1, value_as_long (arg2));
2207 break;
2208
2209 default:
2210 if (type->name ())
2211 error (_("cannot subscript something of type `%s'"),
2212 type->name ());
2213 else
2214 error (_("cannot subscript requested type"));
2215 }
2216 }
2217 }
2218 return (arg1);
2219 }
2220
2221 namespace expr
2222 {
2223
2224 value *
2225 objc_msgcall_operation::evaluate (struct type *expect_type,
2226 struct expression *exp,
2227 enum noside noside)
2228 {
2229 enum noside sub_no_side = EVAL_NORMAL;
2230 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2231
2232 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2233 sub_no_side = EVAL_NORMAL;
2234 else
2235 sub_no_side = noside;
2236 value *target
2237 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side);
2238
2239 if (value_as_long (target) == 0)
2240 sub_no_side = EVAL_AVOID_SIDE_EFFECTS;
2241 else
2242 sub_no_side = noside;
2243 std::vector<operation_up> &args = std::get<2> (m_storage);
2244 value **argvec = XALLOCAVEC (struct value *, args.size () + 3);
2245 argvec[0] = nullptr;
2246 argvec[1] = nullptr;
2247 for (int i = 0; i < args.size (); ++i)
2248 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side);
2249 argvec[args.size () + 2] = nullptr;
2250
2251 return eval_op_objc_msgcall (expect_type, exp, noside, std::
2252 get<0> (m_storage), target,
2253 gdb::make_array_view (argvec,
2254 args.size () + 3));
2255 }
2256
2257 value *
2258 multi_subscript_operation::evaluate (struct type *expect_type,
2259 struct expression *exp,
2260 enum noside noside)
2261 {
2262 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
2263 std::vector<operation_up> &values = std::get<1> (m_storage);
2264 value **argvec = XALLOCAVEC (struct value *, values.size ());
2265 for (int ix = 0; ix < values.size (); ++ix)
2266 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside);
2267 return eval_multi_subscript (expect_type, exp, noside, arg1,
2268 gdb::make_array_view (argvec, values.size ()));
2269 }
2270
2271 value *
2272 logical_and_operation::evaluate (struct type *expect_type,
2273 struct expression *exp,
2274 enum noside noside)
2275 {
2276 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2277
2278 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2279 EVAL_AVOID_SIDE_EFFECTS);
2280
2281 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2))
2282 {
2283 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2284 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside);
2285 }
2286 else
2287 {
2288 bool tem = value_logical_not (arg1);
2289 if (!tem)
2290 {
2291 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2292 tem = value_logical_not (arg2);
2293 }
2294 struct type *type = language_bool_type (exp->language_defn,
2295 exp->gdbarch);
2296 return value_from_longest (type, !tem);
2297 }
2298 }
2299
2300 value *
2301 logical_or_operation::evaluate (struct type *expect_type,
2302 struct expression *exp,
2303 enum noside noside)
2304 {
2305 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2306
2307 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2308 EVAL_AVOID_SIDE_EFFECTS);
2309
2310 if (binop_user_defined_p (BINOP_LOGICAL_OR, arg1, arg2))
2311 {
2312 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2313 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, OP_NULL, noside);
2314 }
2315 else
2316 {
2317 bool tem = value_logical_not (arg1);
2318 if (tem)
2319 {
2320 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2321 tem = value_logical_not (arg2);
2322 }
2323
2324 struct type *type = language_bool_type (exp->language_defn,
2325 exp->gdbarch);
2326 return value_from_longest (type, !tem);
2327 }
2328 }
2329
2330 value *
2331 adl_func_operation::evaluate (struct type *expect_type,
2332 struct expression *exp,
2333 enum noside noside)
2334 {
2335 std::vector<operation_up> &arg_ops = std::get<2> (m_storage);
2336 std::vector<value *> args (arg_ops.size ());
2337 for (int i = 0; i < arg_ops.size (); ++i)
2338 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside);
2339
2340 struct symbol *symp;
2341 find_overload_match (args, std::get<0> (m_storage).c_str (),
2342 NON_METHOD,
2343 nullptr, nullptr,
2344 nullptr, &symp, nullptr, 0, noside);
2345 if (symp->type ()->code () == TYPE_CODE_ERROR)
2346 error_unknown_type (symp->print_name ());
2347 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
2348 return evaluate_subexp_do_call (exp, noside, callee, args,
2349 nullptr, expect_type);
2350
2351 }
2352
2353 /* This function evaluates brace-initializers (in C/C++) for
2354 structure types. */
2355
2356 struct value *
2357 array_operation::evaluate_struct_tuple (struct value *struct_val,
2358 struct expression *exp,
2359 enum noside noside, int nargs)
2360 {
2361 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2362 struct type *struct_type = check_typedef (struct_val->type ());
2363 struct type *field_type;
2364 int fieldno = -1;
2365
2366 int idx = 0;
2367 while (--nargs >= 0)
2368 {
2369 struct value *val = NULL;
2370 int bitpos, bitsize;
2371 bfd_byte *addr;
2372
2373 fieldno++;
2374 /* Skip static fields. */
2375 while (fieldno < struct_type->num_fields ()
2376 && struct_type->field (fieldno).is_static ())
2377 fieldno++;
2378 if (fieldno >= struct_type->num_fields ())
2379 error (_("too many initializers"));
2380 field_type = struct_type->field (fieldno).type ();
2381 if (field_type->code () == TYPE_CODE_UNION
2382 && struct_type->field (fieldno).name ()[0] == '0')
2383 error (_("don't know which variant you want to set"));
2384
2385 /* Here, struct_type is the type of the inner struct,
2386 while substruct_type is the type of the inner struct.
2387 These are the same for normal structures, but a variant struct
2388 contains anonymous union fields that contain substruct fields.
2389 The value fieldno is the index of the top-level (normal or
2390 anonymous union) field in struct_field, while the value
2391 subfieldno is the index of the actual real (named inner) field
2392 in substruct_type. */
2393
2394 field_type = struct_type->field (fieldno).type ();
2395 if (val == 0)
2396 val = in_args[idx++]->evaluate (field_type, exp, noside);
2397
2398 /* Now actually set the field in struct_val. */
2399
2400 /* Assign val to field fieldno. */
2401 if (val->type () != field_type)
2402 val = value_cast (field_type, val);
2403
2404 bitsize = struct_type->field (fieldno).bitsize ();
2405 bitpos = struct_type->field (fieldno).loc_bitpos ();
2406 addr = struct_val->contents_writeable ().data () + bitpos / 8;
2407 if (bitsize)
2408 modify_field (struct_type, addr,
2409 value_as_long (val), bitpos % 8, bitsize);
2410 else
2411 memcpy (addr, val->contents ().data (),
2412 val->type ()->length ());
2413
2414 }
2415 return struct_val;
2416 }
2417
2418 value *
2419 array_operation::evaluate (struct type *expect_type,
2420 struct expression *exp,
2421 enum noside noside)
2422 {
2423 const int provided_low_bound = std::get<0> (m_storage);
2424 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2425 const int nargs = std::get<1> (m_storage) - provided_low_bound + 1;
2426 struct type *type = expect_type ? check_typedef (expect_type) : nullptr;
2427
2428 if (expect_type != nullptr
2429 && type->code () == TYPE_CODE_STRUCT)
2430 {
2431 struct value *rec = value::allocate (expect_type);
2432
2433 memset (rec->contents_raw ().data (), '\0', type->length ());
2434 return evaluate_struct_tuple (rec, exp, noside, nargs);
2435 }
2436
2437 if (expect_type != nullptr
2438 && type->code () == TYPE_CODE_ARRAY)
2439 {
2440 struct type *range_type = type->index_type ();
2441 struct type *element_type = type->target_type ();
2442 struct value *array = value::allocate (expect_type);
2443 int element_size = check_typedef (element_type)->length ();
2444 LONGEST low_bound, high_bound;
2445
2446 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
2447 {
2448 low_bound = 0;
2449 high_bound = (type->length () / element_size) - 1;
2450 }
2451 if (low_bound + nargs - 1 > high_bound)
2452 error (_("Too many array elements"));
2453 memset (array->contents_raw ().data (), 0, expect_type->length ());
2454 for (int idx = 0; idx < nargs; ++idx)
2455 {
2456 struct value *element;
2457
2458 element = in_args[idx]->evaluate (element_type, exp, noside);
2459 if (element->type () != element_type)
2460 element = value_cast (element_type, element);
2461 memcpy (array->contents_raw ().data () + idx * element_size,
2462 element->contents ().data (),
2463 element_size);
2464 }
2465 return array;
2466 }
2467
2468 if (expect_type != nullptr
2469 && type->code () == TYPE_CODE_SET)
2470 {
2471 struct value *set = value::allocate (expect_type);
2472 gdb_byte *valaddr = set->contents_raw ().data ();
2473 struct type *element_type = type->index_type ();
2474 struct type *check_type = element_type;
2475 LONGEST low_bound, high_bound;
2476
2477 /* Get targettype of elementtype. */
2478 while (check_type->code () == TYPE_CODE_RANGE
2479 || check_type->code () == TYPE_CODE_TYPEDEF)
2480 check_type = check_type->target_type ();
2481
2482 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
2483 error (_("(power)set type with unknown size"));
2484 memset (valaddr, '\0', type->length ());
2485 for (int idx = 0; idx < nargs; idx++)
2486 {
2487 LONGEST range_low, range_high;
2488 struct type *range_low_type, *range_high_type;
2489 struct value *elem_val;
2490
2491 elem_val = in_args[idx]->evaluate (element_type, exp, noside);
2492 range_low_type = range_high_type = elem_val->type ();
2493 range_low = range_high = value_as_long (elem_val);
2494
2495 /* Check types of elements to avoid mixture of elements from
2496 different types. Also check if type of element is "compatible"
2497 with element type of powerset. */
2498 if (range_low_type->code () == TYPE_CODE_RANGE)
2499 range_low_type = range_low_type->target_type ();
2500 if (range_high_type->code () == TYPE_CODE_RANGE)
2501 range_high_type = range_high_type->target_type ();
2502 if ((range_low_type->code () != range_high_type->code ())
2503 || (range_low_type->code () == TYPE_CODE_ENUM
2504 && (range_low_type != range_high_type)))
2505 /* different element modes. */
2506 error (_("POWERSET tuple elements of different mode"));
2507 if ((check_type->code () != range_low_type->code ())
2508 || (check_type->code () == TYPE_CODE_ENUM
2509 && range_low_type != check_type))
2510 error (_("incompatible POWERSET tuple elements"));
2511 if (range_low > range_high)
2512 {
2513 warning (_("empty POWERSET tuple range"));
2514 continue;
2515 }
2516 if (range_low < low_bound || range_high > high_bound)
2517 error (_("POWERSET tuple element out of range"));
2518 range_low -= low_bound;
2519 range_high -= low_bound;
2520 for (; range_low <= range_high; range_low++)
2521 {
2522 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
2523
2524 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
2525 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
2526 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
2527 |= 1 << bit_index;
2528 }
2529 }
2530 return set;
2531 }
2532
2533 std::vector<value *> argvec (nargs);
2534 for (int tem = 0; tem < nargs; tem++)
2535 {
2536 /* Ensure that array expressions are coerced into pointer
2537 objects. */
2538 argvec[tem] = in_args[tem]->evaluate_with_coercion (exp, noside);
2539 }
2540 return value_array (provided_low_bound, argvec);
2541 }
2542
2543 value *
2544 unop_extract_operation::evaluate (struct type *expect_type,
2545 struct expression *exp,
2546 enum noside noside)
2547 {
2548 value *old_value = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2549 struct type *type = get_type ();
2550
2551 if (type->length () > old_value->type ()->length ())
2552 error (_("length type is larger than the value type"));
2553
2554 struct value *result = value::allocate (type);
2555 old_value->contents_copy (result, 0, 0, type->length ());
2556 return result;
2557 }
2558
2559 }
2560
2561 /* Helper for evaluate_subexp_for_address. */
2562
2563 static value *
2564 evaluate_subexp_for_address_base (enum noside noside, value *x)
2565 {
2566 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2567 {
2568 struct type *type = check_typedef (x->type ());
2569 enum type_code typecode = type->code ();
2570
2571 if (TYPE_IS_REFERENCE (type))
2572 return value::zero (lookup_pointer_type (type->target_type ()),
2573 not_lval);
2574 else if (x->lval () == lval_memory || value_must_coerce_to_target (x)
2575 || typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
2576 return value::zero (lookup_pointer_type (x->type ()), not_lval);
2577 else
2578 error (_("Attempt to take address of value not located in memory."));
2579 }
2580
2581 return value_addr (x);
2582 }
2583
2584 namespace expr
2585 {
2586
2587 value *
2588 operation::evaluate_for_cast (struct type *expect_type,
2589 struct expression *exp,
2590 enum noside noside)
2591 {
2592 value *val = evaluate (expect_type, exp, noside);
2593 return value_cast (expect_type, val);
2594 }
2595
2596 value *
2597 operation::evaluate_for_address (struct expression *exp, enum noside noside)
2598 {
2599 value *val = evaluate (nullptr, exp, noside);
2600 return evaluate_subexp_for_address_base (noside, val);
2601 }
2602
2603 value *
2604 scope_operation::evaluate_internal (struct type *expect_type,
2605 struct expression *exp,
2606 enum noside noside,
2607 bool want_address)
2608 {
2609 const char *string = std::get<1> (m_storage).c_str ();
2610 value *x = value_aggregate_elt (std::get<0> (m_storage), string,
2611 expect_type, want_address, noside);
2612 if (x == nullptr)
2613 error (_("There is no field named %s"), string);
2614 return x;
2615 }
2616
2617 value *
2618 unop_ind_base_operation::evaluate_for_address (struct expression *exp,
2619 enum noside noside)
2620 {
2621 value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2622
2623 /* We can't optimize out "&*" if there's a user-defined operator*. */
2624 if (unop_user_defined_p (UNOP_IND, x))
2625 {
2626 x = value_x_unop (x, UNOP_IND, noside);
2627 return evaluate_subexp_for_address_base (noside, x);
2628 }
2629
2630 return coerce_array (x);
2631 }
2632
2633 value *
2634 var_msym_value_operation::evaluate_for_address (struct expression *exp,
2635 enum noside noside)
2636 {
2637 const bound_minimal_symbol &b = std::get<0> (m_storage);
2638 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2639 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2640 {
2641 struct type *type = lookup_pointer_type (val->type ());
2642 return value::zero (type, not_lval);
2643 }
2644 else
2645 return value_addr (val);
2646 }
2647
2648 value *
2649 unop_memval_operation::evaluate_for_address (struct expression *exp,
2650 enum noside noside)
2651 {
2652 return value_cast (lookup_pointer_type (std::get<1> (m_storage)),
2653 std::get<0> (m_storage)->evaluate (nullptr, exp, noside));
2654 }
2655
2656 value *
2657 unop_memval_type_operation::evaluate_for_address (struct expression *exp,
2658 enum noside noside)
2659 {
2660 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2661 EVAL_AVOID_SIDE_EFFECTS);
2662 struct type *type = typeval->type ();
2663 return value_cast (lookup_pointer_type (type),
2664 std::get<1> (m_storage)->evaluate (nullptr, exp, noside));
2665 }
2666
2667 value *
2668 var_value_operation::evaluate_for_address (struct expression *exp,
2669 enum noside noside)
2670 {
2671 symbol *var = std::get<0> (m_storage).symbol;
2672
2673 /* C++: The "address" of a reference should yield the address
2674 * of the object pointed to. Let value_addr() deal with it. */
2675 if (TYPE_IS_REFERENCE (var->type ()))
2676 return operation::evaluate_for_address (exp, noside);
2677
2678 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2679 {
2680 struct type *type = lookup_pointer_type (var->type ());
2681 enum address_class sym_class = var->aclass ();
2682
2683 if (sym_class == LOC_CONST
2684 || sym_class == LOC_CONST_BYTES
2685 || sym_class == LOC_REGISTER)
2686 error (_("Attempt to take address of register or constant."));
2687
2688 return value::zero (type, not_lval);
2689 }
2690 else
2691 return address_of_variable (var, std::get<0> (m_storage).block);
2692 }
2693
2694 value *
2695 var_value_operation::evaluate_with_coercion (struct expression *exp,
2696 enum noside noside)
2697 {
2698 struct symbol *var = std::get<0> (m_storage).symbol;
2699 struct type *type = check_typedef (var->type ());
2700 if (type->code () == TYPE_CODE_ARRAY
2701 && !type->is_vector ()
2702 && CAST_IS_CONVERSION (exp->language_defn))
2703 {
2704 struct value *val = address_of_variable (var,
2705 std::get<0> (m_storage).block);
2706 return value_cast (lookup_pointer_type (type->target_type ()), val);
2707 }
2708 return evaluate (nullptr, exp, noside);
2709 }
2710
2711 }
2712
2713 /* Helper function for evaluating the size of a type. */
2714
2715 static value *
2716 evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type)
2717 {
2718 /* FIXME: This should be size_t. */
2719 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2720 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2721 "When applied to a reference or a reference type, the result is
2722 the size of the referenced type." */
2723 type = check_typedef (type);
2724 if (exp->language_defn->la_language == language_cplus
2725 && (TYPE_IS_REFERENCE (type)))
2726 type = check_typedef (type->target_type ());
2727 else if (exp->language_defn->la_language == language_fortran
2728 && type->code () == TYPE_CODE_PTR)
2729 {
2730 /* Dereference Fortran pointer types to allow them for the Fortran
2731 sizeof intrinsic. */
2732 type = check_typedef (type->target_type ());
2733 }
2734 return value_from_longest (size_type, (LONGEST) type->length ());
2735 }
2736
2737 namespace expr
2738 {
2739
2740 value *
2741 operation::evaluate_for_sizeof (struct expression *exp, enum noside noside)
2742 {
2743 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
2744 return evaluate_subexp_for_sizeof_base (exp, val->type ());
2745 }
2746
2747 value *
2748 var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
2749 enum noside noside)
2750
2751 {
2752 const bound_minimal_symbol &b = std::get<0> (m_storage);
2753 value *mval = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2754
2755 struct type *type = mval->type ();
2756 if (type->code () == TYPE_CODE_ERROR)
2757 error_unknown_type (b.minsym->print_name ());
2758
2759 /* FIXME: This should be size_t. */
2760 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2761 return value_from_longest (size_type, type->length ());
2762 }
2763
2764 value *
2765 subscript_operation::evaluate_for_sizeof (struct expression *exp,
2766 enum noside noside)
2767 {
2768 if (noside == EVAL_NORMAL)
2769 {
2770 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2771 EVAL_AVOID_SIDE_EFFECTS);
2772 struct type *type = check_typedef (val->type ());
2773 if (type->code () == TYPE_CODE_ARRAY)
2774 {
2775 type = check_typedef (type->target_type ());
2776 if (type->code () == TYPE_CODE_ARRAY)
2777 {
2778 type = type->index_type ();
2779 /* Only re-evaluate the right hand side if the resulting type
2780 is a variable length type. */
2781 if (type->bounds ()->flag_bound_evaluated)
2782 {
2783 val = evaluate (nullptr, exp, EVAL_NORMAL);
2784 /* FIXME: This should be size_t. */
2785 struct type *size_type
2786 = builtin_type (exp->gdbarch)->builtin_int;
2787 return value_from_longest
2788 (size_type, (LONGEST) val->type ()->length ());
2789 }
2790 }
2791 }
2792 }
2793
2794 return operation::evaluate_for_sizeof (exp, noside);
2795 }
2796
2797 value *
2798 unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
2799 enum noside noside)
2800 {
2801 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2802 EVAL_AVOID_SIDE_EFFECTS);
2803 struct type *type = check_typedef (val->type ());
2804 if (!type->is_pointer_or_reference ()
2805 && type->code () != TYPE_CODE_ARRAY)
2806 error (_("Attempt to take contents of a non-pointer value."));
2807 type = type->target_type ();
2808 if (is_dynamic_type (type))
2809 type = value_ind (val)->type ();
2810 /* FIXME: This should be size_t. */
2811 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2812 return value_from_longest (size_type, (LONGEST) type->length ());
2813 }
2814
2815 value *
2816 unop_memval_operation::evaluate_for_sizeof (struct expression *exp,
2817 enum noside noside)
2818 {
2819 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage));
2820 }
2821
2822 value *
2823 unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
2824 enum noside noside)
2825 {
2826 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2827 EVAL_AVOID_SIDE_EFFECTS);
2828 return evaluate_subexp_for_sizeof_base (exp, typeval->type ());
2829 }
2830
2831 value *
2832 var_value_operation::evaluate_for_sizeof (struct expression *exp,
2833 enum noside noside)
2834 {
2835 struct type *type = std::get<0> (m_storage).symbol->type ();
2836 if (is_dynamic_type (type))
2837 {
2838 value *val = evaluate (nullptr, exp, EVAL_NORMAL);
2839 type = val->type ();
2840 if (type->code () == TYPE_CODE_ARRAY)
2841 {
2842 /* FIXME: This should be size_t. */
2843 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2844 if (type_not_allocated (type) || type_not_associated (type))
2845 return value::zero (size_type, not_lval);
2846 else if (is_dynamic_type (type->index_type ())
2847 && !type->bounds ()->high.is_available ())
2848 return value::allocate_optimized_out (size_type);
2849 }
2850 }
2851 return evaluate_subexp_for_sizeof_base (exp, type);
2852 }
2853
2854 value *
2855 var_msym_value_operation::evaluate_for_cast (struct type *to_type,
2856 struct expression *exp,
2857 enum noside noside)
2858 {
2859 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2860 return value::zero (to_type, not_lval);
2861
2862 const bound_minimal_symbol &b = std::get<0> (m_storage);
2863 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2864
2865 val = value_cast (to_type, val);
2866
2867 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2868 if (val->lval () == lval_memory)
2869 {
2870 if (val->lazy ())
2871 val->fetch_lazy ();
2872 val->set_lval (not_lval);
2873 }
2874 return val;
2875 }
2876
2877 value *
2878 var_value_operation::evaluate_for_cast (struct type *to_type,
2879 struct expression *exp,
2880 enum noside noside)
2881 {
2882 value *val = evaluate_var_value (noside,
2883 std::get<0> (m_storage).block,
2884 std::get<0> (m_storage).symbol);
2885
2886 val = value_cast (to_type, val);
2887
2888 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2889 if (val->lval () == lval_memory)
2890 {
2891 if (val->lazy ())
2892 val->fetch_lazy ();
2893 val->set_lval (not_lval);
2894 }
2895 return val;
2896 }
2897
2898 }
2899
2900 /* Parse a type expression in the string [P..P+LENGTH). */
2901
2902 struct type *
2903 parse_and_eval_type (const char *p, int length)
2904 {
2905 char *tmp = (char *) alloca (length + 4);
2906
2907 tmp[0] = '(';
2908 memcpy (tmp + 1, p, length);
2909 tmp[length + 1] = ')';
2910 tmp[length + 2] = '0';
2911 tmp[length + 3] = '\0';
2912 expression_up expr = parse_expression (tmp);
2913 expr::unop_cast_operation *op
2914 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ());
2915 if (op == nullptr)
2916 error (_("Internal error in eval_type."));
2917 return op->get_type ();
2918 }