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