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