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