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