Add an expr::operation_up to struct expression
[deliverable/binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "value.h"
24 #include "expression.h"
25 #include "target.h"
26 #include "frame.h"
27 #include "gdbthread.h"
28 #include "language.h" /* For CAST_IS_CONVERSION. */
29 #include "cp-abi.h"
30 #include "infcall.h"
31 #include "objc-lang.h"
32 #include "block.h"
33 #include "parser-defs.h"
34 #include "cp-support.h"
35 #include "ui-out.h"
36 #include "regcache.h"
37 #include "user-regs.h"
38 #include "valprint.h"
39 #include "gdb_obstack.h"
40 #include "objfiles.h"
41 #include "typeprint.h"
42 #include <ctype.h>
43 #include "expop.h"
44 #include "c-exp.h"
45
46 /* Prototypes for local functions. */
47
48 static struct value *evaluate_subexp_for_sizeof (struct expression *, int *,
49 enum noside);
50
51 static struct value *evaluate_subexp_for_address (struct expression *,
52 int *, enum noside);
53
54 static value *evaluate_subexp_for_cast (expression *exp, int *pos,
55 enum noside noside,
56 struct type *type);
57
58 static struct value *evaluate_struct_tuple (struct value *,
59 struct expression *, int *,
60 enum noside, int);
61
62 struct value *
63 evaluate_subexp (struct type *expect_type, struct expression *exp,
64 int *pos, enum noside noside)
65 {
66 return ((*exp->language_defn->expression_ops ()->evaluate_exp)
67 (expect_type, exp, pos, noside));
68 }
69 \f
70 /* Parse the string EXP as a C expression, evaluate it,
71 and return the result as a number. */
72
73 CORE_ADDR
74 parse_and_eval_address (const char *exp)
75 {
76 expression_up expr = parse_expression (exp);
77
78 return value_as_address (evaluate_expression (expr.get ()));
79 }
80
81 /* Like parse_and_eval_address, but treats the value of the expression
82 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
83 LONGEST
84 parse_and_eval_long (const char *exp)
85 {
86 expression_up expr = parse_expression (exp);
87
88 return value_as_long (evaluate_expression (expr.get ()));
89 }
90
91 struct value *
92 parse_and_eval (const char *exp)
93 {
94 expression_up expr = parse_expression (exp);
95
96 return evaluate_expression (expr.get ());
97 }
98
99 /* Parse up to a comma (or to a closeparen)
100 in the string EXPP as an expression, evaluate it, and return the value.
101 EXPP is advanced to point to the comma. */
102
103 struct value *
104 parse_to_comma_and_eval (const char **expp)
105 {
106 expression_up expr = parse_exp_1 (expp, 0, nullptr, 1);
107
108 return evaluate_expression (expr.get ());
109 }
110 \f
111
112 /* See expression.h. */
113
114 struct value *
115 expression::evaluate (struct type *expect_type, enum noside noside)
116 {
117 gdb::optional<enable_thread_stack_temporaries> stack_temporaries;
118 if (target_has_execution ()
119 && language_defn->la_language == language_cplus
120 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
121 stack_temporaries.emplace (inferior_thread ());
122
123 struct value *retval;
124 if (op != nullptr)
125 retval = op->evaluate (expect_type, this, noside);
126 else
127 {
128 int pos = 0;
129 retval = evaluate_subexp (expect_type, this, &pos, noside);
130 }
131
132 if (stack_temporaries.has_value ()
133 && value_in_thread_stack_temporaries (retval, inferior_thread ()))
134 retval = value_non_lval (retval);
135
136 return retval;
137 }
138
139 /* See value.h. */
140
141 struct value *
142 evaluate_expression (struct expression *exp, struct type *expect_type)
143 {
144 return exp->evaluate (expect_type, EVAL_NORMAL);
145 }
146
147 /* Evaluate an expression, avoiding all memory references
148 and getting a value whose type alone is correct. */
149
150 struct value *
151 evaluate_type (struct expression *exp)
152 {
153 return exp->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS);
154 }
155
156 /* Evaluate a subexpression, avoiding all memory references and
157 getting a value whose type alone is correct. */
158
159 struct value *
160 evaluate_subexpression_type (struct expression *exp, int subexp)
161 {
162 if (exp->op != nullptr)
163 return exp->op->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
164 return evaluate_subexp (nullptr, exp, &subexp, EVAL_AVOID_SIDE_EFFECTS);
165 }
166
167 /* Find the current value of a watchpoint on EXP. Return the value in
168 *VALP and *RESULTP and the chain of intermediate and final values
169 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
170 not need them.
171
172 If PRESERVE_ERRORS is true, then exceptions are passed through.
173 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
174 occurs while evaluating the expression, *RESULTP will be set to
175 NULL. *RESULTP may be a lazy value, if the result could not be
176 read from memory. It is used to determine whether a value is
177 user-specified (we should watch the whole value) or intermediate
178 (we should watch only the bit used to locate the final value).
179
180 If the final value, or any intermediate value, could not be read
181 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
182 set to any referenced values. *VALP will never be a lazy value.
183 This is the value which we store in struct breakpoint.
184
185 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
186 released from the value chain. If VAL_CHAIN is NULL, all generated
187 values will be left on the value chain. */
188
189 void
190 fetch_subexp_value (struct expression *exp, int *pc,
191 expr::operation *op,
192 struct value **valp, struct value **resultp,
193 std::vector<value_ref_ptr> *val_chain,
194 bool preserve_errors)
195 {
196 struct value *mark, *new_mark, *result;
197
198 *valp = NULL;
199 if (resultp)
200 *resultp = NULL;
201 if (val_chain)
202 val_chain->clear ();
203
204 /* Evaluate the expression. */
205 mark = value_mark ();
206 result = NULL;
207
208 try
209 {
210 if (op == nullptr)
211 result = evaluate_subexp (nullptr, exp, pc, EVAL_NORMAL);
212 else
213 result = op->evaluate (nullptr, exp, EVAL_NORMAL);
214 }
215 catch (const gdb_exception &ex)
216 {
217 /* Ignore memory errors if we want watchpoints pointing at
218 inaccessible memory to still be created; otherwise, throw the
219 error to some higher catcher. */
220 switch (ex.error)
221 {
222 case MEMORY_ERROR:
223 if (!preserve_errors)
224 break;
225 /* Fall through. */
226 default:
227 throw;
228 break;
229 }
230 }
231
232 new_mark = value_mark ();
233 if (mark == new_mark)
234 return;
235 if (resultp)
236 *resultp = result;
237
238 /* Make sure it's not lazy, so that after the target stops again we
239 have a non-lazy previous value to compare with. */
240 if (result != NULL)
241 {
242 if (!value_lazy (result))
243 *valp = result;
244 else
245 {
246
247 try
248 {
249 value_fetch_lazy (result);
250 *valp = result;
251 }
252 catch (const gdb_exception_error &except)
253 {
254 }
255 }
256 }
257
258 if (val_chain)
259 {
260 /* Return the chain of intermediate values. We use this to
261 decide which addresses to watch. */
262 *val_chain = value_release_to_mark (mark);
263 }
264 }
265
266 /* Extract a field operation from an expression. If the subexpression
267 of EXP starting at *SUBEXP is not a structure dereference
268 operation, return NULL. Otherwise, return the name of the
269 dereferenced field, and advance *SUBEXP to point to the
270 subexpression of the left-hand-side of the dereference. This is
271 used when completing field names. */
272
273 const char *
274 extract_field_op (struct expression *exp, int *subexp)
275 {
276 int tem;
277 char *result;
278
279 if (exp->elts[*subexp].opcode != STRUCTOP_STRUCT
280 && exp->elts[*subexp].opcode != STRUCTOP_PTR)
281 return NULL;
282 tem = longest_to_int (exp->elts[*subexp + 1].longconst);
283 result = &exp->elts[*subexp + 2].string;
284 (*subexp) += 1 + 3 + BYTES_TO_EXP_ELEM (tem + 1);
285 return result;
286 }
287
288 /* This function evaluates brace-initializers (in C/C++) for
289 structure types. */
290
291 static struct value *
292 evaluate_struct_tuple (struct value *struct_val,
293 struct expression *exp,
294 int *pos, enum noside noside, int nargs)
295 {
296 struct type *struct_type = check_typedef (value_type (struct_val));
297 struct type *field_type;
298 int fieldno = -1;
299
300 while (--nargs >= 0)
301 {
302 struct value *val = NULL;
303 int bitpos, bitsize;
304 bfd_byte *addr;
305
306 fieldno++;
307 /* Skip static fields. */
308 while (fieldno < struct_type->num_fields ()
309 && field_is_static (&struct_type->field (fieldno)))
310 fieldno++;
311 if (fieldno >= struct_type->num_fields ())
312 error (_("too many initializers"));
313 field_type = struct_type->field (fieldno).type ();
314 if (field_type->code () == TYPE_CODE_UNION
315 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
316 error (_("don't know which variant you want to set"));
317
318 /* Here, struct_type is the type of the inner struct,
319 while substruct_type is the type of the inner struct.
320 These are the same for normal structures, but a variant struct
321 contains anonymous union fields that contain substruct fields.
322 The value fieldno is the index of the top-level (normal or
323 anonymous union) field in struct_field, while the value
324 subfieldno is the index of the actual real (named inner) field
325 in substruct_type. */
326
327 field_type = struct_type->field (fieldno).type ();
328 if (val == 0)
329 val = evaluate_subexp (field_type, exp, pos, noside);
330
331 /* Now actually set the field in struct_val. */
332
333 /* Assign val to field fieldno. */
334 if (value_type (val) != field_type)
335 val = value_cast (field_type, val);
336
337 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
338 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
339 addr = value_contents_writeable (struct_val) + bitpos / 8;
340 if (bitsize)
341 modify_field (struct_type, addr,
342 value_as_long (val), bitpos % 8, bitsize);
343 else
344 memcpy (addr, value_contents (val),
345 TYPE_LENGTH (value_type (val)));
346
347 }
348 return struct_val;
349 }
350
351 /* Promote value ARG1 as appropriate before performing a unary operation
352 on this argument.
353 If the result is not appropriate for any particular language then it
354 needs to patch this function. */
355
356 void
357 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
358 struct value **arg1)
359 {
360 struct type *type1;
361
362 *arg1 = coerce_ref (*arg1);
363 type1 = check_typedef (value_type (*arg1));
364
365 if (is_integral_type (type1))
366 {
367 switch (language->la_language)
368 {
369 default:
370 /* Perform integral promotion for ANSI C/C++.
371 If not appropriate for any particular language
372 it needs to modify this function. */
373 {
374 struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
375
376 if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_int))
377 *arg1 = value_cast (builtin_int, *arg1);
378 }
379 break;
380 }
381 }
382 }
383
384 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
385 operation on those two operands.
386 If the result is not appropriate for any particular language then it
387 needs to patch this function. */
388
389 void
390 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
391 struct value **arg1, struct value **arg2)
392 {
393 struct type *promoted_type = NULL;
394 struct type *type1;
395 struct type *type2;
396
397 *arg1 = coerce_ref (*arg1);
398 *arg2 = coerce_ref (*arg2);
399
400 type1 = check_typedef (value_type (*arg1));
401 type2 = check_typedef (value_type (*arg2));
402
403 if ((type1->code () != TYPE_CODE_FLT
404 && type1->code () != TYPE_CODE_DECFLOAT
405 && !is_integral_type (type1))
406 || (type2->code () != TYPE_CODE_FLT
407 && type2->code () != TYPE_CODE_DECFLOAT
408 && !is_integral_type (type2)))
409 return;
410
411 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
412 return;
413
414 if (type1->code () == TYPE_CODE_DECFLOAT
415 || type2->code () == TYPE_CODE_DECFLOAT)
416 {
417 /* No promotion required. */
418 }
419 else if (type1->code () == TYPE_CODE_FLT
420 || type2->code () == TYPE_CODE_FLT)
421 {
422 switch (language->la_language)
423 {
424 case language_c:
425 case language_cplus:
426 case language_asm:
427 case language_objc:
428 case language_opencl:
429 /* No promotion required. */
430 break;
431
432 default:
433 /* For other languages the result type is unchanged from gdb
434 version 6.7 for backward compatibility.
435 If either arg was long double, make sure that value is also long
436 double. Otherwise use double. */
437 if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (gdbarch)
438 || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (gdbarch))
439 promoted_type = builtin_type (gdbarch)->builtin_long_double;
440 else
441 promoted_type = builtin_type (gdbarch)->builtin_double;
442 break;
443 }
444 }
445 else if (type1->code () == TYPE_CODE_BOOL
446 && type2->code () == TYPE_CODE_BOOL)
447 {
448 /* No promotion required. */
449 }
450 else
451 /* Integral operations here. */
452 /* FIXME: Also mixed integral/booleans, with result an integer. */
453 {
454 const struct builtin_type *builtin = builtin_type (gdbarch);
455 unsigned int promoted_len1 = TYPE_LENGTH (type1);
456 unsigned int promoted_len2 = TYPE_LENGTH (type2);
457 int is_unsigned1 = type1->is_unsigned ();
458 int is_unsigned2 = type2->is_unsigned ();
459 unsigned int result_len;
460 int unsigned_operation;
461
462 /* Determine type length and signedness after promotion for
463 both operands. */
464 if (promoted_len1 < TYPE_LENGTH (builtin->builtin_int))
465 {
466 is_unsigned1 = 0;
467 promoted_len1 = TYPE_LENGTH (builtin->builtin_int);
468 }
469 if (promoted_len2 < TYPE_LENGTH (builtin->builtin_int))
470 {
471 is_unsigned2 = 0;
472 promoted_len2 = TYPE_LENGTH (builtin->builtin_int);
473 }
474
475 if (promoted_len1 > promoted_len2)
476 {
477 unsigned_operation = is_unsigned1;
478 result_len = promoted_len1;
479 }
480 else if (promoted_len2 > promoted_len1)
481 {
482 unsigned_operation = is_unsigned2;
483 result_len = promoted_len2;
484 }
485 else
486 {
487 unsigned_operation = is_unsigned1 || is_unsigned2;
488 result_len = promoted_len1;
489 }
490
491 switch (language->la_language)
492 {
493 case language_c:
494 case language_cplus:
495 case language_asm:
496 case language_objc:
497 if (result_len <= TYPE_LENGTH (builtin->builtin_int))
498 {
499 promoted_type = (unsigned_operation
500 ? builtin->builtin_unsigned_int
501 : builtin->builtin_int);
502 }
503 else if (result_len <= TYPE_LENGTH (builtin->builtin_long))
504 {
505 promoted_type = (unsigned_operation
506 ? builtin->builtin_unsigned_long
507 : builtin->builtin_long);
508 }
509 else
510 {
511 promoted_type = (unsigned_operation
512 ? builtin->builtin_unsigned_long_long
513 : builtin->builtin_long_long);
514 }
515 break;
516 case language_opencl:
517 if (result_len <= TYPE_LENGTH (lookup_signed_typename
518 (language, "int")))
519 {
520 promoted_type =
521 (unsigned_operation
522 ? lookup_unsigned_typename (language, "int")
523 : lookup_signed_typename (language, "int"));
524 }
525 else if (result_len <= TYPE_LENGTH (lookup_signed_typename
526 (language, "long")))
527 {
528 promoted_type =
529 (unsigned_operation
530 ? lookup_unsigned_typename (language, "long")
531 : lookup_signed_typename (language,"long"));
532 }
533 break;
534 default:
535 /* For other languages the result type is unchanged from gdb
536 version 6.7 for backward compatibility.
537 If either arg was long long, make sure that value is also long
538 long. Otherwise use long. */
539 if (unsigned_operation)
540 {
541 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
542 promoted_type = builtin->builtin_unsigned_long_long;
543 else
544 promoted_type = builtin->builtin_unsigned_long;
545 }
546 else
547 {
548 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
549 promoted_type = builtin->builtin_long_long;
550 else
551 promoted_type = builtin->builtin_long;
552 }
553 break;
554 }
555 }
556
557 if (promoted_type)
558 {
559 /* Promote both operands to common type. */
560 *arg1 = value_cast (promoted_type, *arg1);
561 *arg2 = value_cast (promoted_type, *arg2);
562 }
563 }
564
565 static int
566 ptrmath_type_p (const struct language_defn *lang, struct type *type)
567 {
568 type = check_typedef (type);
569 if (TYPE_IS_REFERENCE (type))
570 type = TYPE_TARGET_TYPE (type);
571
572 switch (type->code ())
573 {
574 case TYPE_CODE_PTR:
575 case TYPE_CODE_FUNC:
576 return 1;
577
578 case TYPE_CODE_ARRAY:
579 return type->is_vector () ? 0 : lang->c_style_arrays_p ();
580
581 default:
582 return 0;
583 }
584 }
585
586 /* Represents a fake method with the given parameter types. This is
587 used by the parser to construct a temporary "expected" type for
588 method overload resolution. FLAGS is used as instance flags of the
589 new type, in order to be able to make the new type represent a
590 const/volatile overload. */
591
592 class fake_method
593 {
594 public:
595 fake_method (type_instance_flags flags,
596 int num_types, struct type **param_types);
597 ~fake_method ();
598
599 /* The constructed type. */
600 struct type *type () { return &m_type; }
601
602 private:
603 struct type m_type {};
604 main_type m_main_type {};
605 };
606
607 fake_method::fake_method (type_instance_flags flags,
608 int num_types, struct type **param_types)
609 {
610 struct type *type = &m_type;
611
612 TYPE_MAIN_TYPE (type) = &m_main_type;
613 TYPE_LENGTH (type) = 1;
614 type->set_code (TYPE_CODE_METHOD);
615 TYPE_CHAIN (type) = type;
616 type->set_instance_flags (flags);
617 if (num_types > 0)
618 {
619 if (param_types[num_types - 1] == NULL)
620 {
621 --num_types;
622 type->set_has_varargs (true);
623 }
624 else if (check_typedef (param_types[num_types - 1])->code ()
625 == TYPE_CODE_VOID)
626 {
627 --num_types;
628 /* Caller should have ensured this. */
629 gdb_assert (num_types == 0);
630 type->set_is_prototyped (true);
631 }
632 }
633
634 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
635 neither an objfile nor a gdbarch. As a result we must manually
636 allocate memory for auxiliary fields, and free the memory ourselves
637 when we are done with it. */
638 type->set_num_fields (num_types);
639 type->set_fields
640 ((struct field *) xzalloc (sizeof (struct field) * num_types));
641
642 while (num_types-- > 0)
643 type->field (num_types).set_type (param_types[num_types]);
644 }
645
646 fake_method::~fake_method ()
647 {
648 xfree (m_type.fields ());
649 }
650
651 namespace expr
652 {
653
654 value *
655 type_instance_operation::evaluate (struct type *expect_type,
656 struct expression *exp,
657 enum noside noside)
658 {
659 type_instance_flags flags = std::get<0> (m_storage);
660 std::vector<type *> &types = std::get<1> (m_storage);
661
662 fake_method fake_expect_type (flags, types.size (), types.data ());
663 return std::get<2> (m_storage)->evaluate (fake_expect_type.type (),
664 exp, noside);
665 }
666
667 }
668
669 /* Helper for evaluating an OP_VAR_VALUE. */
670
671 value *
672 evaluate_var_value (enum noside noside, const block *blk, symbol *var)
673 {
674 /* JYG: We used to just return value_zero of the symbol type if
675 we're asked to avoid side effects. Otherwise we return
676 value_of_variable (...). However I'm not sure if
677 value_of_variable () has any side effect. We need a full value
678 object returned here for whatis_exp () to call evaluate_type ()
679 and then pass the full value to value_rtti_target_type () if we
680 are dealing with a pointer or reference to a base class and print
681 object is on. */
682
683 struct value *ret = NULL;
684
685 try
686 {
687 ret = value_of_variable (var, blk);
688 }
689
690 catch (const gdb_exception_error &except)
691 {
692 if (noside != EVAL_AVOID_SIDE_EFFECTS)
693 throw;
694
695 ret = value_zero (SYMBOL_TYPE (var), not_lval);
696 }
697
698 return ret;
699 }
700
701 namespace expr
702
703 {
704
705 value *
706 var_value_operation::evaluate (struct type *expect_type,
707 struct expression *exp,
708 enum noside noside)
709 {
710 symbol *var = std::get<0> (m_storage);
711 if (SYMBOL_TYPE (var)->code () == TYPE_CODE_ERROR)
712 error_unknown_type (var->print_name ());
713 return evaluate_var_value (noside, std::get<1> (m_storage), var);
714 }
715
716 } /* namespace expr */
717
718 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
719
720 value *
721 evaluate_var_msym_value (enum noside noside,
722 struct objfile *objfile, minimal_symbol *msymbol)
723 {
724 CORE_ADDR address;
725 type *the_type = find_minsym_type_and_address (msymbol, objfile, &address);
726
727 if (noside == EVAL_AVOID_SIDE_EFFECTS && !the_type->is_gnu_ifunc ())
728 return value_zero (the_type, not_lval);
729 else
730 return value_at_lazy (the_type, address);
731 }
732
733 /* Helper for returning a value when handling EVAL_SKIP. */
734
735 value *
736 eval_skip_value (expression *exp)
737 {
738 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
739 }
740
741 /* See expression.h. */
742
743 value *
744 evaluate_subexp_do_call (expression *exp, enum noside noside,
745 value *callee,
746 gdb::array_view<value *> argvec,
747 const char *function_name,
748 type *default_return_type)
749 {
750 if (callee == NULL)
751 error (_("Cannot evaluate function -- may be inlined"));
752 if (noside == EVAL_AVOID_SIDE_EFFECTS)
753 {
754 /* If the return type doesn't look like a function type,
755 call an error. This can happen if somebody tries to turn
756 a variable into a function call. */
757
758 type *ftype = value_type (callee);
759
760 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
761 {
762 /* We don't know anything about what the internal
763 function might return, but we have to return
764 something. */
765 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
766 not_lval);
767 }
768 else if (ftype->code () == TYPE_CODE_XMETHOD)
769 {
770 type *return_type = result_type_of_xmethod (callee, argvec);
771
772 if (return_type == NULL)
773 error (_("Xmethod is missing return type."));
774 return value_zero (return_type, not_lval);
775 }
776 else if (ftype->code () == TYPE_CODE_FUNC
777 || ftype->code () == TYPE_CODE_METHOD)
778 {
779 if (ftype->is_gnu_ifunc ())
780 {
781 CORE_ADDR address = value_address (callee);
782 type *resolved_type = find_gnu_ifunc_target_type (address);
783
784 if (resolved_type != NULL)
785 ftype = resolved_type;
786 }
787
788 type *return_type = TYPE_TARGET_TYPE (ftype);
789
790 if (return_type == NULL)
791 return_type = default_return_type;
792
793 if (return_type == NULL)
794 error_call_unknown_return_type (function_name);
795
796 return allocate_value (return_type);
797 }
798 else
799 error (_("Expression of type other than "
800 "\"Function returning ...\" used as function"));
801 }
802 switch (value_type (callee)->code ())
803 {
804 case TYPE_CODE_INTERNAL_FUNCTION:
805 return call_internal_function (exp->gdbarch, exp->language_defn,
806 callee, argvec.size (), argvec.data ());
807 case TYPE_CODE_XMETHOD:
808 return call_xmethod (callee, argvec);
809 default:
810 return call_function_by_hand (callee, default_return_type, argvec);
811 }
812 }
813
814 /* Helper for evaluating an OP_FUNCALL. */
815
816 static value *
817 evaluate_funcall (type *expect_type, expression *exp, int *pos,
818 enum noside noside)
819 {
820 int tem;
821 int pc2 = 0;
822 value *arg1 = NULL;
823 value *arg2 = NULL;
824 int save_pos1;
825 symbol *function = NULL;
826 char *function_name = NULL;
827 const char *var_func_name = NULL;
828
829 int pc = (*pos);
830 (*pos) += 2;
831
832 exp_opcode op = exp->elts[*pos].opcode;
833 int nargs = longest_to_int (exp->elts[pc].longconst);
834 /* Allocate arg vector, including space for the function to be
835 called in argvec[0], a potential `this', and a terminating
836 NULL. */
837 value **argvec = (value **) alloca (sizeof (value *) * (nargs + 3));
838 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
839 {
840 /* First, evaluate the structure into arg2. */
841 pc2 = (*pos)++;
842
843 if (op == STRUCTOP_MEMBER)
844 {
845 arg2 = evaluate_subexp_for_address (exp, pos, noside);
846 }
847 else
848 {
849 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
850 }
851
852 /* If the function is a virtual function, then the aggregate
853 value (providing the structure) plays its part by providing
854 the vtable. Otherwise, it is just along for the ride: call
855 the function directly. */
856
857 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
858
859 type *a1_type = check_typedef (value_type (arg1));
860 if (noside == EVAL_SKIP)
861 tem = 1; /* Set it to the right arg index so that all
862 arguments can also be skipped. */
863 else if (a1_type->code () == TYPE_CODE_METHODPTR)
864 {
865 if (noside == EVAL_AVOID_SIDE_EFFECTS)
866 arg1 = value_zero (TYPE_TARGET_TYPE (a1_type), not_lval);
867 else
868 arg1 = cplus_method_ptr_to_value (&arg2, arg1);
869
870 /* Now, say which argument to start evaluating from. */
871 nargs++;
872 tem = 2;
873 argvec[1] = arg2;
874 }
875 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
876 {
877 struct type *type_ptr
878 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
879 struct type *target_type_ptr
880 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type));
881
882 /* Now, convert these values to an address. */
883 arg2 = value_cast (type_ptr, arg2);
884
885 long mem_offset = value_as_long (arg1);
886
887 arg1 = value_from_pointer (target_type_ptr,
888 value_as_long (arg2) + mem_offset);
889 arg1 = value_ind (arg1);
890 tem = 1;
891 }
892 else
893 error (_("Non-pointer-to-member value used in pointer-to-member "
894 "construct"));
895 }
896 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
897 {
898 /* Hair for method invocations. */
899 int tem2;
900
901 nargs++;
902 /* First, evaluate the structure into arg2. */
903 pc2 = (*pos)++;
904 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
905 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
906
907 if (op == STRUCTOP_STRUCT)
908 {
909 /* If v is a variable in a register, and the user types
910 v.method (), this will produce an error, because v has no
911 address.
912
913 A possible way around this would be to allocate a copy of
914 the variable on the stack, copy in the contents, call the
915 function, and copy out the contents. I.e. convert this
916 from call by reference to call by copy-return (or
917 whatever it's called). However, this does not work
918 because it is not the same: the method being called could
919 stash a copy of the address, and then future uses through
920 that address (after the method returns) would be expected
921 to use the variable itself, not some copy of it. */
922 arg2 = evaluate_subexp_for_address (exp, pos, noside);
923 }
924 else
925 {
926 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
927
928 /* Check to see if the operator '->' has been overloaded.
929 If the operator has been overloaded replace arg2 with the
930 value returned by the custom operator and continue
931 evaluation. */
932 while (unop_user_defined_p (op, arg2))
933 {
934 struct value *value = NULL;
935 try
936 {
937 value = value_x_unop (arg2, op, noside);
938 }
939
940 catch (const gdb_exception_error &except)
941 {
942 if (except.error == NOT_FOUND_ERROR)
943 break;
944 else
945 throw;
946 }
947
948 arg2 = value;
949 }
950 }
951 /* Now, say which argument to start evaluating from. */
952 tem = 2;
953 }
954 else if (op == OP_SCOPE
955 && overload_resolution
956 && (exp->language_defn->la_language == language_cplus))
957 {
958 /* Unpack it locally so we can properly handle overload
959 resolution. */
960 char *name;
961 int local_tem;
962
963 pc2 = (*pos)++;
964 local_tem = longest_to_int (exp->elts[pc2 + 2].longconst);
965 (*pos) += 4 + BYTES_TO_EXP_ELEM (local_tem + 1);
966 struct type *type = exp->elts[pc2 + 1].type;
967 name = &exp->elts[pc2 + 3].string;
968
969 function = NULL;
970 function_name = NULL;
971 if (type->code () == TYPE_CODE_NAMESPACE)
972 {
973 function = cp_lookup_symbol_namespace (type->name (),
974 name,
975 get_selected_block (0),
976 VAR_DOMAIN).symbol;
977 if (function == NULL)
978 error (_("No symbol \"%s\" in namespace \"%s\"."),
979 name, type->name ());
980
981 tem = 1;
982 /* arg2 is left as NULL on purpose. */
983 }
984 else
985 {
986 gdb_assert (type->code () == TYPE_CODE_STRUCT
987 || type->code () == TYPE_CODE_UNION);
988 function_name = name;
989
990 /* We need a properly typed value for method lookup. For
991 static methods arg2 is otherwise unused. */
992 arg2 = value_zero (type, lval_memory);
993 ++nargs;
994 tem = 2;
995 }
996 }
997 else if (op == OP_ADL_FUNC)
998 {
999 /* Save the function position and move pos so that the arguments
1000 can be evaluated. */
1001 int func_name_len;
1002
1003 save_pos1 = *pos;
1004 tem = 1;
1005
1006 func_name_len = longest_to_int (exp->elts[save_pos1 + 3].longconst);
1007 (*pos) += 6 + BYTES_TO_EXP_ELEM (func_name_len + 1);
1008 }
1009 else
1010 {
1011 /* Non-method function call. */
1012 save_pos1 = *pos;
1013 tem = 1;
1014
1015 /* If this is a C++ function wait until overload resolution. */
1016 if (op == OP_VAR_VALUE
1017 && overload_resolution
1018 && (exp->language_defn->la_language == language_cplus))
1019 {
1020 (*pos) += 4; /* Skip the evaluation of the symbol. */
1021 argvec[0] = NULL;
1022 }
1023 else
1024 {
1025 if (op == OP_VAR_MSYM_VALUE)
1026 {
1027 minimal_symbol *msym = exp->elts[*pos + 2].msymbol;
1028 var_func_name = msym->print_name ();
1029 }
1030 else if (op == OP_VAR_VALUE)
1031 {
1032 symbol *sym = exp->elts[*pos + 2].symbol;
1033 var_func_name = sym->print_name ();
1034 }
1035
1036 argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
1037 type *type = value_type (argvec[0]);
1038 if (type && type->code () == TYPE_CODE_PTR)
1039 type = TYPE_TARGET_TYPE (type);
1040 if (type && type->code () == TYPE_CODE_FUNC)
1041 {
1042 for (; tem <= nargs && tem <= type->num_fields (); tem++)
1043 {
1044 argvec[tem] = evaluate_subexp (type->field (tem - 1).type (),
1045 exp, pos, noside);
1046 }
1047 }
1048 }
1049 }
1050
1051 /* Evaluate arguments (if not already done, e.g., namespace::func()
1052 and overload-resolution is off). */
1053 for (; tem <= nargs; tem++)
1054 {
1055 /* Ensure that array expressions are coerced into pointer
1056 objects. */
1057 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1058 }
1059
1060 /* Signal end of arglist. */
1061 argvec[tem] = 0;
1062
1063 if (noside == EVAL_SKIP)
1064 return eval_skip_value (exp);
1065
1066 if (op == OP_ADL_FUNC)
1067 {
1068 struct symbol *symp;
1069 char *func_name;
1070 int name_len;
1071 int string_pc = save_pos1 + 3;
1072
1073 /* Extract the function name. */
1074 name_len = longest_to_int (exp->elts[string_pc].longconst);
1075 func_name = (char *) alloca (name_len + 1);
1076 strcpy (func_name, &exp->elts[string_pc + 1].string);
1077
1078 find_overload_match (gdb::make_array_view (&argvec[1], nargs),
1079 func_name,
1080 NON_METHOD, /* not method */
1081 NULL, NULL, /* pass NULL symbol since
1082 symbol is unknown */
1083 NULL, &symp, NULL, 0, noside);
1084
1085 /* Now fix the expression being evaluated. */
1086 exp->elts[save_pos1 + 2].symbol = symp;
1087 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside);
1088 }
1089
1090 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR
1091 || (op == OP_SCOPE && function_name != NULL))
1092 {
1093 int static_memfuncp;
1094 char *tstr;
1095
1096 /* Method invocation: stuff "this" as first parameter. If the
1097 method turns out to be static we undo this below. */
1098 argvec[1] = arg2;
1099
1100 if (op != OP_SCOPE)
1101 {
1102 /* Name of method from expression. */
1103 tstr = &exp->elts[pc2 + 2].string;
1104 }
1105 else
1106 tstr = function_name;
1107
1108 if (overload_resolution && (exp->language_defn->la_language
1109 == language_cplus))
1110 {
1111 /* Language is C++, do some overload resolution before
1112 evaluation. */
1113 struct value *valp = NULL;
1114
1115 (void) find_overload_match (gdb::make_array_view (&argvec[1], nargs),
1116 tstr,
1117 METHOD, /* method */
1118 &arg2, /* the object */
1119 NULL, &valp, NULL,
1120 &static_memfuncp, 0, noside);
1121
1122 if (op == OP_SCOPE && !static_memfuncp)
1123 {
1124 /* For the time being, we don't handle this. */
1125 error (_("Call to overloaded function %s requires "
1126 "`this' pointer"),
1127 function_name);
1128 }
1129 argvec[1] = arg2; /* the ``this'' pointer */
1130 argvec[0] = valp; /* Use the method found after overload
1131 resolution. */
1132 }
1133 else
1134 /* Non-C++ case -- or no overload resolution. */
1135 {
1136 struct value *temp = arg2;
1137
1138 argvec[0] = value_struct_elt (&temp, argvec + 1, tstr,
1139 &static_memfuncp,
1140 op == STRUCTOP_STRUCT
1141 ? "structure" : "structure pointer");
1142 /* value_struct_elt updates temp with the correct value of
1143 the ``this'' pointer if necessary, so modify argvec[1] to
1144 reflect any ``this'' changes. */
1145 arg2
1146 = value_from_longest (lookup_pointer_type(value_type (temp)),
1147 value_address (temp)
1148 + value_embedded_offset (temp));
1149 argvec[1] = arg2; /* the ``this'' pointer */
1150 }
1151
1152 /* Take out `this' if needed. */
1153 if (static_memfuncp)
1154 {
1155 argvec[1] = argvec[0];
1156 nargs--;
1157 argvec++;
1158 }
1159 }
1160 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1161 {
1162 /* Pointer to member. argvec[1] is already set up. */
1163 argvec[0] = arg1;
1164 }
1165 else if (op == OP_VAR_VALUE || (op == OP_SCOPE && function != NULL))
1166 {
1167 /* Non-member function being called. */
1168 /* fn: This can only be done for C++ functions. A C-style
1169 function in a C++ program, for instance, does not have the
1170 fields that are expected here. */
1171
1172 if (overload_resolution && (exp->language_defn->la_language
1173 == language_cplus))
1174 {
1175 /* Language is C++, do some overload resolution before
1176 evaluation. */
1177 struct symbol *symp;
1178 int no_adl = 0;
1179
1180 /* If a scope has been specified disable ADL. */
1181 if (op == OP_SCOPE)
1182 no_adl = 1;
1183
1184 if (op == OP_VAR_VALUE)
1185 function = exp->elts[save_pos1+2].symbol;
1186
1187 (void) find_overload_match (gdb::make_array_view (&argvec[1], nargs),
1188 NULL, /* no need for name */
1189 NON_METHOD, /* not method */
1190 NULL, function, /* the function */
1191 NULL, &symp, NULL, no_adl, noside);
1192
1193 if (op == OP_VAR_VALUE)
1194 {
1195 /* Now fix the expression being evaluated. */
1196 exp->elts[save_pos1+2].symbol = symp;
1197 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1,
1198 noside);
1199 }
1200 else
1201 argvec[0] = value_of_variable (symp, get_selected_block (0));
1202 }
1203 else
1204 {
1205 /* Not C++, or no overload resolution allowed. */
1206 /* Nothing to be done; argvec already correctly set up. */
1207 }
1208 }
1209 else
1210 {
1211 /* It is probably a C-style function. */
1212 /* Nothing to be done; argvec already correctly set up. */
1213 }
1214
1215 return evaluate_subexp_do_call (exp, noside, argvec[0],
1216 gdb::make_array_view (argvec + 1, nargs),
1217 var_func_name, expect_type);
1218 }
1219
1220 namespace expr
1221 {
1222
1223 value *
1224 operation::evaluate_funcall (struct type *expect_type,
1225 struct expression *exp,
1226 enum noside noside,
1227 const char *function_name,
1228 const std::vector<operation_up> &args)
1229 {
1230 std::vector<value *> vals (args.size ());
1231
1232 value *callee = evaluate_with_coercion (exp, noside);
1233 for (int i = 0; i < args.size (); ++i)
1234 vals[i] = args[i]->evaluate_with_coercion (exp, noside);
1235
1236 return evaluate_subexp_do_call (exp, noside, callee, vals,
1237 function_name, expect_type);
1238 }
1239
1240 value *
1241 var_value_operation::evaluate_funcall (struct type *expect_type,
1242 struct expression *exp,
1243 enum noside noside,
1244 const std::vector<operation_up> &args)
1245 {
1246 if (!overload_resolution
1247 || exp->language_defn->la_language != language_cplus)
1248 return operation::evaluate_funcall (expect_type, exp, noside, args);
1249
1250 std::vector<value *> argvec (args.size ());
1251 for (int i = 0; i < args.size (); ++i)
1252 argvec[i] = args[i]->evaluate_with_coercion (exp, noside);
1253
1254 struct symbol *symp;
1255 find_overload_match (argvec, NULL, NON_METHOD,
1256 NULL, std::get<0> (m_storage),
1257 NULL, &symp, NULL, 0, noside);
1258
1259 if (SYMBOL_TYPE (symp)->code () == TYPE_CODE_ERROR)
1260 error_unknown_type (symp->print_name ());
1261 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
1262
1263 return evaluate_subexp_do_call (exp, noside, callee, argvec,
1264 nullptr, expect_type);
1265 }
1266
1267 value *
1268 scope_operation::evaluate_funcall (struct type *expect_type,
1269 struct expression *exp,
1270 enum noside noside,
1271 const std::vector<operation_up> &args)
1272 {
1273 if (!overload_resolution
1274 || exp->language_defn->la_language != language_cplus)
1275 return operation::evaluate_funcall (expect_type, exp, noside, args);
1276
1277 /* Unpack it locally so we can properly handle overload
1278 resolution. */
1279 const std::string &name = std::get<1> (m_storage);
1280 struct type *type = std::get<0> (m_storage);
1281
1282 symbol *function = NULL;
1283 const char *function_name = NULL;
1284 std::vector<value *> argvec (1 + args.size ());
1285 if (type->code () == TYPE_CODE_NAMESPACE)
1286 {
1287 function = cp_lookup_symbol_namespace (type->name (),
1288 name.c_str (),
1289 get_selected_block (0),
1290 VAR_DOMAIN).symbol;
1291 if (function == NULL)
1292 error (_("No symbol \"%s\" in namespace \"%s\"."),
1293 name.c_str (), type->name ());
1294 }
1295 else
1296 {
1297 gdb_assert (type->code () == TYPE_CODE_STRUCT
1298 || type->code () == TYPE_CODE_UNION);
1299 function_name = name.c_str ();
1300
1301 /* We need a properly typed value for method lookup. */
1302 argvec[0] = value_zero (type, lval_memory);
1303 }
1304
1305 for (int i = 0; i < args.size (); ++i)
1306 argvec[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
1307 gdb::array_view<value *> arg_view = argvec;
1308
1309 value *callee = nullptr;
1310 if (function_name != nullptr)
1311 {
1312 int static_memfuncp;
1313
1314 find_overload_match (arg_view, function_name, METHOD,
1315 &argvec[0], nullptr, &callee, nullptr,
1316 &static_memfuncp, 0, noside);
1317 if (!static_memfuncp)
1318 {
1319 /* For the time being, we don't handle this. */
1320 error (_("Call to overloaded function %s requires "
1321 "`this' pointer"),
1322 function_name);
1323 }
1324
1325 arg_view = arg_view.slice (1);
1326 }
1327 else
1328 {
1329 symbol *symp;
1330 arg_view = arg_view.slice (1);
1331 find_overload_match (arg_view, nullptr,
1332 NON_METHOD, nullptr, function,
1333 nullptr, &symp, nullptr, 1, noside);
1334 callee = value_of_variable (symp, get_selected_block (0));
1335 }
1336
1337 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
1338 nullptr, expect_type);
1339 }
1340
1341 value *
1342 structop_member_base::evaluate_funcall (struct type *expect_type,
1343 struct expression *exp,
1344 enum noside noside,
1345 const std::vector<operation_up> &args)
1346 {
1347 /* First, evaluate the structure into lhs. */
1348 value *lhs;
1349 if (opcode () == STRUCTOP_MEMBER)
1350 lhs = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
1351 else
1352 lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1353
1354 std::vector<value *> vals (args.size () + 1);
1355 gdb::array_view<value *> val_view = vals;
1356 /* If the function is a virtual function, then the aggregate
1357 value (providing the structure) plays its part by providing
1358 the vtable. Otherwise, it is just along for the ride: call
1359 the function directly. */
1360 value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1361 value *callee;
1362
1363 type *a1_type = check_typedef (value_type (rhs));
1364 if (a1_type->code () == TYPE_CODE_METHODPTR)
1365 {
1366 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1367 callee = value_zero (TYPE_TARGET_TYPE (a1_type), not_lval);
1368 else
1369 callee = cplus_method_ptr_to_value (&lhs, rhs);
1370
1371 vals[0] = lhs;
1372 }
1373 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
1374 {
1375 struct type *type_ptr
1376 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
1377 struct type *target_type_ptr
1378 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type));
1379
1380 /* Now, convert this value to an address. */
1381 lhs = value_cast (type_ptr, lhs);
1382
1383 long mem_offset = value_as_long (rhs);
1384
1385 callee = value_from_pointer (target_type_ptr,
1386 value_as_long (lhs) + mem_offset);
1387 callee = value_ind (callee);
1388
1389 val_view = val_view.slice (1);
1390 }
1391 else
1392 error (_("Non-pointer-to-member value used in pointer-to-member "
1393 "construct"));
1394
1395 for (int i = 0; i < args.size (); ++i)
1396 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
1397
1398 return evaluate_subexp_do_call (exp, noside, callee, val_view,
1399 nullptr, expect_type);
1400
1401 }
1402
1403 value *
1404 structop_base_operation::evaluate_funcall
1405 (struct type *expect_type, struct expression *exp, enum noside noside,
1406 const std::vector<operation_up> &args)
1407 {
1408 std::vector<value *> vals (args.size () + 1);
1409 /* First, evaluate the structure into vals[0]. */
1410 enum exp_opcode op = opcode ();
1411 if (op == STRUCTOP_STRUCT)
1412 {
1413 /* If v is a variable in a register, and the user types
1414 v.method (), this will produce an error, because v has no
1415 address.
1416
1417 A possible way around this would be to allocate a copy of
1418 the variable on the stack, copy in the contents, call the
1419 function, and copy out the contents. I.e. convert this
1420 from call by reference to call by copy-return (or
1421 whatever it's called). However, this does not work
1422 because it is not the same: the method being called could
1423 stash a copy of the address, and then future uses through
1424 that address (after the method returns) would be expected
1425 to use the variable itself, not some copy of it. */
1426 vals[0] = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
1427 }
1428 else
1429 {
1430 vals[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1431 /* Check to see if the operator '->' has been overloaded.
1432 If the operator has been overloaded replace vals[0] with the
1433 value returned by the custom operator and continue
1434 evaluation. */
1435 while (unop_user_defined_p (op, vals[0]))
1436 {
1437 struct value *value = nullptr;
1438 try
1439 {
1440 value = value_x_unop (vals[0], op, noside);
1441 }
1442 catch (const gdb_exception_error &except)
1443 {
1444 if (except.error == NOT_FOUND_ERROR)
1445 break;
1446 else
1447 throw;
1448 }
1449
1450 vals[0] = value;
1451 }
1452 }
1453
1454 for (int i = 0; i < args.size (); ++i)
1455 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
1456 gdb::array_view<value *> arg_view = vals;
1457
1458 int static_memfuncp;
1459 value *callee;
1460 const char *tstr = std::get<1> (m_storage).c_str ();
1461 if (overload_resolution
1462 && exp->language_defn->la_language == language_cplus)
1463 {
1464 /* Language is C++, do some overload resolution before
1465 evaluation. */
1466 value *val0 = vals[0];
1467 find_overload_match (arg_view, tstr, METHOD,
1468 &val0, nullptr, &callee, nullptr,
1469 &static_memfuncp, 0, noside);
1470 vals[0] = val0;
1471 }
1472 else
1473 /* Non-C++ case -- or no overload resolution. */
1474 {
1475 struct value *temp = vals[0];
1476
1477 callee = value_struct_elt (&temp, &vals[1], tstr,
1478 &static_memfuncp,
1479 op == STRUCTOP_STRUCT
1480 ? "structure" : "structure pointer");
1481 /* value_struct_elt updates temp with the correct value of the
1482 ``this'' pointer if necessary, so modify it to reflect any
1483 ``this'' changes. */
1484 vals[0] = value_from_longest (lookup_pointer_type (value_type (temp)),
1485 value_address (temp)
1486 + value_embedded_offset (temp));
1487 }
1488
1489 /* Take out `this' if needed. */
1490 if (static_memfuncp)
1491 arg_view = arg_view.slice (1);
1492
1493 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
1494 nullptr, expect_type);
1495 }
1496
1497
1498 } /* namespace expr */
1499
1500 /* Return true if type is integral or reference to integral */
1501
1502 static bool
1503 is_integral_or_integral_reference (struct type *type)
1504 {
1505 if (is_integral_type (type))
1506 return true;
1507
1508 type = check_typedef (type);
1509 return (type != nullptr
1510 && TYPE_IS_REFERENCE (type)
1511 && is_integral_type (TYPE_TARGET_TYPE (type)));
1512 }
1513
1514 /* Helper function that implements the body of OP_SCOPE. */
1515
1516 struct value *
1517 eval_op_scope (struct type *expect_type, struct expression *exp,
1518 enum noside noside,
1519 struct type *type, const char *string)
1520 {
1521 if (noside == EVAL_SKIP)
1522 return eval_skip_value (exp);
1523 struct value *arg1 = value_aggregate_elt (type, string, expect_type,
1524 0, noside);
1525 if (arg1 == NULL)
1526 error (_("There is no field named %s"), string);
1527 return arg1;
1528 }
1529
1530 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1531
1532 struct value *
1533 eval_op_var_entry_value (struct type *expect_type, struct expression *exp,
1534 enum noside noside, symbol *sym)
1535 {
1536 if (noside == EVAL_SKIP)
1537 return eval_skip_value (exp);
1538 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1539 return value_zero (SYMBOL_TYPE (sym), not_lval);
1540
1541 if (SYMBOL_COMPUTED_OPS (sym) == NULL
1542 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
1543 error (_("Symbol \"%s\" does not have any specific entry value"),
1544 sym->print_name ());
1545
1546 struct frame_info *frame = get_selected_frame (NULL);
1547 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
1548 }
1549
1550 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1551
1552 struct value *
1553 eval_op_var_msym_value (struct type *expect_type, struct expression *exp,
1554 enum noside noside, bool outermost_p,
1555 minimal_symbol *msymbol, struct objfile *objfile)
1556 {
1557 value *val = evaluate_var_msym_value (noside, objfile, msymbol);
1558
1559 struct type *type = value_type (val);
1560 if (type->code () == TYPE_CODE_ERROR
1561 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p))
1562 error_unknown_type (msymbol->print_name ());
1563 return val;
1564 }
1565
1566 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1567
1568 struct value *
1569 eval_op_func_static_var (struct type *expect_type, struct expression *exp,
1570 enum noside noside,
1571 value *func, const char *var)
1572 {
1573 if (noside == EVAL_SKIP)
1574 return eval_skip_value (exp);
1575 CORE_ADDR addr = value_address (func);
1576 const block *blk = block_for_pc (addr);
1577 struct block_symbol sym = lookup_symbol (var, blk, VAR_DOMAIN, NULL);
1578 if (sym.symbol == NULL)
1579 error (_("No symbol \"%s\" in specified context."), var);
1580 return evaluate_var_value (noside, sym.block, sym.symbol);
1581 }
1582
1583 /* Helper function that implements the body of OP_REGISTER. */
1584
1585 struct value *
1586 eval_op_register (struct type *expect_type, struct expression *exp,
1587 enum noside noside, const char *name)
1588 {
1589 int regno;
1590 struct value *val;
1591
1592 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1593 name, strlen (name));
1594 if (regno == -1)
1595 error (_("Register $%s not available."), name);
1596
1597 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1598 a value with the appropriate register type. Unfortunately,
1599 we don't have easy access to the type of user registers.
1600 So for these registers, we fetch the register value regardless
1601 of the evaluation mode. */
1602 if (noside == EVAL_AVOID_SIDE_EFFECTS
1603 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
1604 val = value_zero (register_type (exp->gdbarch, regno), not_lval);
1605 else
1606 val = value_of_register (regno, get_selected_frame (NULL));
1607 if (val == NULL)
1608 error (_("Value of register %s not available."), name);
1609 else
1610 return val;
1611 }
1612
1613 /* Helper function that implements the body of OP_STRING. */
1614
1615 struct value *
1616 eval_op_string (struct type *expect_type, struct expression *exp,
1617 enum noside noside, int len, const char *string)
1618 {
1619 if (noside == EVAL_SKIP)
1620 return eval_skip_value (exp);
1621 struct type *type = language_string_char_type (exp->language_defn,
1622 exp->gdbarch);
1623 return value_string (string, len, type);
1624 }
1625
1626 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1627
1628 struct value *
1629 eval_op_objc_selector (struct type *expect_type, struct expression *exp,
1630 enum noside noside,
1631 const char *sel)
1632 {
1633 if (noside == EVAL_SKIP)
1634 return eval_skip_value (exp);
1635
1636 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1637 return value_from_longest (selector_type,
1638 lookup_child_selector (exp->gdbarch, sel));
1639 }
1640
1641 /* Helper function that implements the body of BINOP_CONCAT. */
1642
1643 struct value *
1644 eval_op_concat (struct type *expect_type, struct expression *exp,
1645 enum noside noside, struct value *arg1, struct value *arg2)
1646 {
1647 if (noside == EVAL_SKIP)
1648 return eval_skip_value (exp);
1649 if (binop_user_defined_p (BINOP_CONCAT, arg1, arg2))
1650 return value_x_binop (arg1, arg2, BINOP_CONCAT, OP_NULL, noside);
1651 else
1652 return value_concat (arg1, arg2);
1653 }
1654
1655 /* A helper function for TERNOP_SLICE. */
1656
1657 struct value *
1658 eval_op_ternop (struct type *expect_type, struct expression *exp,
1659 enum noside noside,
1660 struct value *array, struct value *low, struct value *upper)
1661 {
1662 if (noside == EVAL_SKIP)
1663 return eval_skip_value (exp);
1664 int lowbound = value_as_long (low);
1665 int upperbound = value_as_long (upper);
1666 return value_slice (array, lowbound, upperbound - lowbound + 1);
1667 }
1668
1669 /* A helper function for STRUCTOP_STRUCT. */
1670
1671 struct value *
1672 eval_op_structop_struct (struct type *expect_type, struct expression *exp,
1673 enum noside noside,
1674 struct value *arg1, const char *string)
1675 {
1676 if (noside == EVAL_SKIP)
1677 return eval_skip_value (exp);
1678 struct value *arg3 = value_struct_elt (&arg1, NULL, string,
1679 NULL, "structure");
1680 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1681 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3));
1682 return arg3;
1683 }
1684
1685 /* A helper function for STRUCTOP_PTR. */
1686
1687 struct value *
1688 eval_op_structop_ptr (struct type *expect_type, struct expression *exp,
1689 enum noside noside,
1690 struct value *arg1, const char *string)
1691 {
1692 if (noside == EVAL_SKIP)
1693 return eval_skip_value (exp);
1694
1695 /* Check to see if operator '->' has been overloaded. If so replace
1696 arg1 with the value returned by evaluating operator->(). */
1697 while (unop_user_defined_p (STRUCTOP_PTR, arg1))
1698 {
1699 struct value *value = NULL;
1700 try
1701 {
1702 value = value_x_unop (arg1, STRUCTOP_PTR, noside);
1703 }
1704
1705 catch (const gdb_exception_error &except)
1706 {
1707 if (except.error == NOT_FOUND_ERROR)
1708 break;
1709 else
1710 throw;
1711 }
1712
1713 arg1 = value;
1714 }
1715
1716 /* JYG: if print object is on we need to replace the base type
1717 with rtti type in order to continue on with successful
1718 lookup of member / method only available in the rtti type. */
1719 {
1720 struct type *arg_type = value_type (arg1);
1721 struct type *real_type;
1722 int full, using_enc;
1723 LONGEST top;
1724 struct value_print_options opts;
1725
1726 get_user_print_options (&opts);
1727 if (opts.objectprint && TYPE_TARGET_TYPE (arg_type)
1728 && (TYPE_TARGET_TYPE (arg_type)->code () == TYPE_CODE_STRUCT))
1729 {
1730 real_type = value_rtti_indirect_type (arg1, &full, &top,
1731 &using_enc);
1732 if (real_type)
1733 arg1 = value_cast (real_type, arg1);
1734 }
1735 }
1736
1737 struct value *arg3 = value_struct_elt (&arg1, NULL, string,
1738 NULL, "structure pointer");
1739 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1740 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3));
1741 return arg3;
1742 }
1743
1744 /* A helper function for STRUCTOP_MEMBER. */
1745
1746 struct value *
1747 eval_op_member (struct type *expect_type, struct expression *exp,
1748 enum noside noside,
1749 struct value *arg1, struct value *arg2)
1750 {
1751 long mem_offset;
1752
1753 if (noside == EVAL_SKIP)
1754 return eval_skip_value (exp);
1755
1756 struct value *arg3;
1757 struct type *type = check_typedef (value_type (arg2));
1758 switch (type->code ())
1759 {
1760 case TYPE_CODE_METHODPTR:
1761 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1762 return value_zero (TYPE_TARGET_TYPE (type), not_lval);
1763 else
1764 {
1765 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1766 gdb_assert (value_type (arg2)->code () == TYPE_CODE_PTR);
1767 return value_ind (arg2);
1768 }
1769
1770 case TYPE_CODE_MEMBERPTR:
1771 /* Now, convert these values to an address. */
1772 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1773 arg1, 1);
1774
1775 mem_offset = value_as_long (arg2);
1776
1777 arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1778 value_as_long (arg1) + mem_offset);
1779 return value_ind (arg3);
1780
1781 default:
1782 error (_("non-pointer-to-member value used "
1783 "in pointer-to-member construct"));
1784 }
1785 }
1786
1787 /* A helper function for BINOP_ADD. */
1788
1789 struct value *
1790 eval_op_add (struct type *expect_type, struct expression *exp,
1791 enum noside noside,
1792 struct value *arg1, struct value *arg2)
1793 {
1794 if (noside == EVAL_SKIP)
1795 return eval_skip_value (exp);
1796 if (binop_user_defined_p (BINOP_ADD, arg1, arg2))
1797 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside);
1798 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
1799 && is_integral_or_integral_reference (value_type (arg2)))
1800 return value_ptradd (arg1, value_as_long (arg2));
1801 else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
1802 && is_integral_or_integral_reference (value_type (arg1)))
1803 return value_ptradd (arg2, value_as_long (arg1));
1804 else
1805 {
1806 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1807 return value_binop (arg1, arg2, BINOP_ADD);
1808 }
1809 }
1810
1811 /* A helper function for BINOP_SUB. */
1812
1813 struct value *
1814 eval_op_sub (struct type *expect_type, struct expression *exp,
1815 enum noside noside,
1816 struct value *arg1, struct value *arg2)
1817 {
1818 if (noside == EVAL_SKIP)
1819 return eval_skip_value (exp);
1820 if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
1821 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
1822 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
1823 && ptrmath_type_p (exp->language_defn, value_type (arg2)))
1824 {
1825 /* FIXME -- should be ptrdiff_t */
1826 struct type *type = builtin_type (exp->gdbarch)->builtin_long;
1827 return value_from_longest (type, value_ptrdiff (arg1, arg2));
1828 }
1829 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
1830 && is_integral_or_integral_reference (value_type (arg2)))
1831 return value_ptradd (arg1, - value_as_long (arg2));
1832 else
1833 {
1834 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1835 return value_binop (arg1, arg2, BINOP_SUB);
1836 }
1837 }
1838
1839 /* Helper function for several different binary operations. */
1840
1841 struct value *
1842 eval_op_binary (struct type *expect_type, struct expression *exp,
1843 enum noside noside, enum exp_opcode op,
1844 struct value *arg1, struct value *arg2)
1845 {
1846 if (noside == EVAL_SKIP)
1847 return eval_skip_value (exp);
1848 if (binop_user_defined_p (op, arg1, arg2))
1849 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1850 else
1851 {
1852 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1853 fudge arg2 to avoid division-by-zero, the caller is
1854 (theoretically) only looking for the type of the result. */
1855 if (noside == EVAL_AVOID_SIDE_EFFECTS
1856 /* ??? Do we really want to test for BINOP_MOD here?
1857 The implementation of value_binop gives it a well-defined
1858 value. */
1859 && (op == BINOP_DIV
1860 || op == BINOP_INTDIV
1861 || op == BINOP_REM
1862 || op == BINOP_MOD)
1863 && value_logical_not (arg2))
1864 {
1865 struct value *v_one;
1866
1867 v_one = value_one (value_type (arg2));
1868 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
1869 return value_binop (arg1, v_one, op);
1870 }
1871 else
1872 {
1873 /* For shift and integer exponentiation operations,
1874 only promote the first argument. */
1875 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1876 && is_integral_type (value_type (arg2)))
1877 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1878 else
1879 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1880
1881 return value_binop (arg1, arg2, op);
1882 }
1883 }
1884 }
1885
1886 /* A helper function for BINOP_SUBSCRIPT. */
1887
1888 struct value *
1889 eval_op_subscript (struct type *expect_type, struct expression *exp,
1890 enum noside noside, enum exp_opcode op,
1891 struct value *arg1, struct value *arg2)
1892 {
1893 if (noside == EVAL_SKIP)
1894 return eval_skip_value (exp);
1895 if (binop_user_defined_p (op, arg1, arg2))
1896 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1897 else
1898 {
1899 /* If the user attempts to subscript something that is not an
1900 array or pointer type (like a plain int variable for example),
1901 then report this as an error. */
1902
1903 arg1 = coerce_ref (arg1);
1904 struct type *type = check_typedef (value_type (arg1));
1905 if (type->code () != TYPE_CODE_ARRAY
1906 && type->code () != TYPE_CODE_PTR)
1907 {
1908 if (type->name ())
1909 error (_("cannot subscript something of type `%s'"),
1910 type->name ());
1911 else
1912 error (_("cannot subscript requested type"));
1913 }
1914
1915 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1916 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
1917 else
1918 return value_subscript (arg1, value_as_long (arg2));
1919 }
1920 }
1921
1922 /* A helper function for BINOP_EQUAL. */
1923
1924 struct value *
1925 eval_op_equal (struct type *expect_type, struct expression *exp,
1926 enum noside noside, enum exp_opcode op,
1927 struct value *arg1, struct value *arg2)
1928 {
1929 if (noside == EVAL_SKIP)
1930 return eval_skip_value (exp);
1931 if (binop_user_defined_p (op, arg1, arg2))
1932 {
1933 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1934 }
1935 else
1936 {
1937 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1938 int tem = value_equal (arg1, arg2);
1939 struct type *type = language_bool_type (exp->language_defn,
1940 exp->gdbarch);
1941 return value_from_longest (type, (LONGEST) tem);
1942 }
1943 }
1944
1945 /* A helper function for BINOP_NOTEQUAL. */
1946
1947 struct value *
1948 eval_op_notequal (struct type *expect_type, struct expression *exp,
1949 enum noside noside, enum exp_opcode op,
1950 struct value *arg1, struct value *arg2)
1951 {
1952 if (noside == EVAL_SKIP)
1953 return eval_skip_value (exp);
1954 if (binop_user_defined_p (op, arg1, arg2))
1955 {
1956 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1957 }
1958 else
1959 {
1960 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1961 int tem = value_equal (arg1, arg2);
1962 struct type *type = language_bool_type (exp->language_defn,
1963 exp->gdbarch);
1964 return value_from_longest (type, (LONGEST) ! tem);
1965 }
1966 }
1967
1968 /* A helper function for BINOP_LESS. */
1969
1970 struct value *
1971 eval_op_less (struct type *expect_type, struct expression *exp,
1972 enum noside noside, enum exp_opcode op,
1973 struct value *arg1, struct value *arg2)
1974 {
1975 if (noside == EVAL_SKIP)
1976 return eval_skip_value (exp);
1977 if (binop_user_defined_p (op, arg1, arg2))
1978 {
1979 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1980 }
1981 else
1982 {
1983 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1984 int tem = value_less (arg1, arg2);
1985 struct type *type = language_bool_type (exp->language_defn,
1986 exp->gdbarch);
1987 return value_from_longest (type, (LONGEST) tem);
1988 }
1989 }
1990
1991 /* A helper function for BINOP_GTR. */
1992
1993 struct value *
1994 eval_op_gtr (struct type *expect_type, struct expression *exp,
1995 enum noside noside, enum exp_opcode op,
1996 struct value *arg1, struct value *arg2)
1997 {
1998 if (noside == EVAL_SKIP)
1999 return eval_skip_value (exp);
2000 if (binop_user_defined_p (op, arg1, arg2))
2001 {
2002 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2003 }
2004 else
2005 {
2006 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2007 int tem = value_less (arg2, arg1);
2008 struct type *type = language_bool_type (exp->language_defn,
2009 exp->gdbarch);
2010 return value_from_longest (type, (LONGEST) tem);
2011 }
2012 }
2013
2014 /* A helper function for BINOP_GEQ. */
2015
2016 struct value *
2017 eval_op_geq (struct type *expect_type, struct expression *exp,
2018 enum noside noside, enum exp_opcode op,
2019 struct value *arg1, struct value *arg2)
2020 {
2021 if (noside == EVAL_SKIP)
2022 return eval_skip_value (exp);
2023 if (binop_user_defined_p (op, arg1, arg2))
2024 {
2025 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2026 }
2027 else
2028 {
2029 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2030 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
2031 struct type *type = language_bool_type (exp->language_defn,
2032 exp->gdbarch);
2033 return value_from_longest (type, (LONGEST) tem);
2034 }
2035 }
2036
2037 /* A helper function for BINOP_LEQ. */
2038
2039 struct value *
2040 eval_op_leq (struct type *expect_type, struct expression *exp,
2041 enum noside noside, enum exp_opcode op,
2042 struct value *arg1, struct value *arg2)
2043 {
2044 if (noside == EVAL_SKIP)
2045 return eval_skip_value (exp);
2046 if (binop_user_defined_p (op, arg1, arg2))
2047 {
2048 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2049 }
2050 else
2051 {
2052 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2053 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
2054 struct type *type = language_bool_type (exp->language_defn,
2055 exp->gdbarch);
2056 return value_from_longest (type, (LONGEST) tem);
2057 }
2058 }
2059
2060 /* A helper function for BINOP_REPEAT. */
2061
2062 struct value *
2063 eval_op_repeat (struct type *expect_type, struct expression *exp,
2064 enum noside noside, enum exp_opcode op,
2065 struct value *arg1, struct value *arg2)
2066 {
2067 if (noside == EVAL_SKIP)
2068 return eval_skip_value (exp);
2069 struct type *type = check_typedef (value_type (arg2));
2070 if (type->code () != TYPE_CODE_INT
2071 && type->code () != TYPE_CODE_ENUM)
2072 error (_("Non-integral right operand for \"@\" operator."));
2073 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2074 {
2075 return allocate_repeat_value (value_type (arg1),
2076 longest_to_int (value_as_long (arg2)));
2077 }
2078 else
2079 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
2080 }
2081
2082 /* A helper function for UNOP_PLUS. */
2083
2084 struct value *
2085 eval_op_plus (struct type *expect_type, struct expression *exp,
2086 enum noside noside, enum exp_opcode op,
2087 struct value *arg1)
2088 {
2089 if (noside == EVAL_SKIP)
2090 return eval_skip_value (exp);
2091 if (unop_user_defined_p (op, arg1))
2092 return value_x_unop (arg1, op, noside);
2093 else
2094 {
2095 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2096 return value_pos (arg1);
2097 }
2098 }
2099
2100 /* A helper function for UNOP_NEG. */
2101
2102 struct value *
2103 eval_op_neg (struct type *expect_type, struct expression *exp,
2104 enum noside noside, enum exp_opcode op,
2105 struct value *arg1)
2106 {
2107 if (noside == EVAL_SKIP)
2108 return eval_skip_value (exp);
2109 if (unop_user_defined_p (op, arg1))
2110 return value_x_unop (arg1, op, noside);
2111 else
2112 {
2113 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2114 return value_neg (arg1);
2115 }
2116 }
2117
2118 /* A helper function for UNOP_COMPLEMENT. */
2119
2120 struct value *
2121 eval_op_complement (struct type *expect_type, struct expression *exp,
2122 enum noside noside, enum exp_opcode op,
2123 struct value *arg1)
2124 {
2125 if (noside == EVAL_SKIP)
2126 return eval_skip_value (exp);
2127 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
2128 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
2129 else
2130 {
2131 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2132 return value_complement (arg1);
2133 }
2134 }
2135
2136 /* A helper function for UNOP_LOGICAL_NOT. */
2137
2138 struct value *
2139 eval_op_lognot (struct type *expect_type, struct expression *exp,
2140 enum noside noside, enum exp_opcode op,
2141 struct value *arg1)
2142 {
2143 if (noside == EVAL_SKIP)
2144 return eval_skip_value (exp);
2145 if (unop_user_defined_p (op, arg1))
2146 return value_x_unop (arg1, op, noside);
2147 else
2148 {
2149 struct type *type = language_bool_type (exp->language_defn,
2150 exp->gdbarch);
2151 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
2152 }
2153 }
2154
2155 /* A helper function for UNOP_IND. */
2156
2157 struct value *
2158 eval_op_ind (struct type *expect_type, struct expression *exp,
2159 enum noside noside,
2160 struct value *arg1)
2161 {
2162 struct type *type = check_typedef (value_type (arg1));
2163 if (type->code () == TYPE_CODE_METHODPTR
2164 || type->code () == TYPE_CODE_MEMBERPTR)
2165 error (_("Attempt to dereference pointer "
2166 "to member without an object"));
2167 if (noside == EVAL_SKIP)
2168 return eval_skip_value (exp);
2169 if (unop_user_defined_p (UNOP_IND, arg1))
2170 return value_x_unop (arg1, UNOP_IND, noside);
2171 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2172 {
2173 type = check_typedef (value_type (arg1));
2174
2175 /* If the type pointed to is dynamic then in order to resolve the
2176 dynamic properties we must actually dereference the pointer.
2177 There is a risk that this dereference will have side-effects
2178 in the inferior, but being able to print accurate type
2179 information seems worth the risk. */
2180 if ((type->code () != TYPE_CODE_PTR
2181 && !TYPE_IS_REFERENCE (type))
2182 || !is_dynamic_type (TYPE_TARGET_TYPE (type)))
2183 {
2184 if (type->code () == TYPE_CODE_PTR
2185 || TYPE_IS_REFERENCE (type)
2186 /* In C you can dereference an array to get the 1st elt. */
2187 || type->code () == TYPE_CODE_ARRAY)
2188 return value_zero (TYPE_TARGET_TYPE (type),
2189 lval_memory);
2190 else if (type->code () == TYPE_CODE_INT)
2191 /* GDB allows dereferencing an int. */
2192 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
2193 lval_memory);
2194 else
2195 error (_("Attempt to take contents of a non-pointer value."));
2196 }
2197 }
2198
2199 /* Allow * on an integer so we can cast it to whatever we want.
2200 This returns an int, which seems like the most C-like thing to
2201 do. "long long" variables are rare enough that
2202 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
2203 if (type->code () == TYPE_CODE_INT)
2204 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
2205 (CORE_ADDR) value_as_address (arg1));
2206 return value_ind (arg1);
2207 }
2208
2209 /* A helper function for UNOP_ALIGNOF. */
2210
2211 struct value *
2212 eval_op_alignof (struct type *expect_type, struct expression *exp,
2213 enum noside noside,
2214 struct value *arg1)
2215 {
2216 struct type *type = value_type (arg1);
2217 /* FIXME: This should be size_t. */
2218 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2219 ULONGEST align = type_align (type);
2220 if (align == 0)
2221 error (_("could not determine alignment of type"));
2222 return value_from_longest (size_type, align);
2223 }
2224
2225 /* A helper function for UNOP_MEMVAL. */
2226
2227 struct value *
2228 eval_op_memval (struct type *expect_type, struct expression *exp,
2229 enum noside noside,
2230 struct value *arg1, struct type *type)
2231 {
2232 if (noside == EVAL_SKIP)
2233 return eval_skip_value (exp);
2234 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2235 return value_zero (type, lval_memory);
2236 else
2237 return value_at_lazy (type, value_as_address (arg1));
2238 }
2239
2240 /* A helper function for UNOP_PREINCREMENT. */
2241
2242 struct value *
2243 eval_op_preinc (struct type *expect_type, struct expression *exp,
2244 enum noside noside, enum exp_opcode op,
2245 struct value *arg1)
2246 {
2247 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2248 return arg1;
2249 else if (unop_user_defined_p (op, arg1))
2250 {
2251 return value_x_unop (arg1, op, noside);
2252 }
2253 else
2254 {
2255 struct value *arg2;
2256 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2257 arg2 = value_ptradd (arg1, 1);
2258 else
2259 {
2260 struct value *tmp = arg1;
2261
2262 arg2 = value_one (value_type (arg1));
2263 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2264 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2265 }
2266
2267 return value_assign (arg1, arg2);
2268 }
2269 }
2270
2271 /* A helper function for UNOP_PREDECREMENT. */
2272
2273 struct value *
2274 eval_op_predec (struct type *expect_type, struct expression *exp,
2275 enum noside noside, enum exp_opcode op,
2276 struct value *arg1)
2277 {
2278 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2279 return arg1;
2280 else if (unop_user_defined_p (op, arg1))
2281 {
2282 return value_x_unop (arg1, op, noside);
2283 }
2284 else
2285 {
2286 struct value *arg2;
2287 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2288 arg2 = value_ptradd (arg1, -1);
2289 else
2290 {
2291 struct value *tmp = arg1;
2292
2293 arg2 = value_one (value_type (arg1));
2294 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2295 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2296 }
2297
2298 return value_assign (arg1, arg2);
2299 }
2300 }
2301
2302 /* A helper function for UNOP_POSTINCREMENT. */
2303
2304 struct value *
2305 eval_op_postinc (struct type *expect_type, struct expression *exp,
2306 enum noside noside, enum exp_opcode op,
2307 struct value *arg1)
2308 {
2309 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2310 return arg1;
2311 else if (unop_user_defined_p (op, arg1))
2312 {
2313 return value_x_unop (arg1, op, noside);
2314 }
2315 else
2316 {
2317 struct value *arg3 = value_non_lval (arg1);
2318 struct value *arg2;
2319
2320 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2321 arg2 = value_ptradd (arg1, 1);
2322 else
2323 {
2324 struct value *tmp = arg1;
2325
2326 arg2 = value_one (value_type (arg1));
2327 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2328 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2329 }
2330
2331 value_assign (arg1, arg2);
2332 return arg3;
2333 }
2334 }
2335
2336 /* A helper function for UNOP_POSTDECREMENT. */
2337
2338 struct value *
2339 eval_op_postdec (struct type *expect_type, struct expression *exp,
2340 enum noside noside, enum exp_opcode op,
2341 struct value *arg1)
2342 {
2343 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2344 return arg1;
2345 else if (unop_user_defined_p (op, arg1))
2346 {
2347 return value_x_unop (arg1, op, noside);
2348 }
2349 else
2350 {
2351 struct value *arg3 = value_non_lval (arg1);
2352 struct value *arg2;
2353
2354 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2355 arg2 = value_ptradd (arg1, -1);
2356 else
2357 {
2358 struct value *tmp = arg1;
2359
2360 arg2 = value_one (value_type (arg1));
2361 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2362 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2363 }
2364
2365 value_assign (arg1, arg2);
2366 return arg3;
2367 }
2368 }
2369
2370 /* A helper function for OP_TYPE. */
2371
2372 struct value *
2373 eval_op_type (struct type *expect_type, struct expression *exp,
2374 enum noside noside, struct type *type)
2375 {
2376 if (noside == EVAL_SKIP)
2377 return eval_skip_value (exp);
2378 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2379 return allocate_value (type);
2380 else
2381 error (_("Attempt to use a type name as an expression"));
2382 }
2383
2384 /* A helper function for BINOP_ASSIGN_MODIFY. */
2385
2386 struct value *
2387 eval_binop_assign_modify (struct type *expect_type, struct expression *exp,
2388 enum noside noside, enum exp_opcode op,
2389 struct value *arg1, struct value *arg2)
2390 {
2391 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2392 return arg1;
2393 if (binop_user_defined_p (op, arg1, arg2))
2394 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
2395 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
2396 value_type (arg1))
2397 && is_integral_type (value_type (arg2)))
2398 arg2 = value_ptradd (arg1, value_as_long (arg2));
2399 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
2400 value_type (arg1))
2401 && is_integral_type (value_type (arg2)))
2402 arg2 = value_ptradd (arg1, - value_as_long (arg2));
2403 else
2404 {
2405 struct value *tmp = arg1;
2406
2407 /* For shift and integer exponentiation operations,
2408 only promote the first argument. */
2409 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2410 && is_integral_type (value_type (arg2)))
2411 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
2412 else
2413 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2414
2415 arg2 = value_binop (tmp, arg2, op);
2416 }
2417 return value_assign (arg1, arg2);
2418 }
2419
2420 /* Note that ARGS needs 2 empty slots up front and must end with a
2421 null pointer. */
2422 static struct value *
2423 eval_op_objc_msgcall (struct type *expect_type, struct expression *exp,
2424 enum noside noside, CORE_ADDR selector,
2425 value *target, gdb::array_view<value *> args)
2426 {
2427 CORE_ADDR responds_selector = 0;
2428 CORE_ADDR method_selector = 0;
2429
2430 int struct_return = 0;
2431
2432 struct value *msg_send = NULL;
2433 struct value *msg_send_stret = NULL;
2434 int gnu_runtime = 0;
2435
2436 struct value *method = NULL;
2437 struct value *called_method = NULL;
2438
2439 struct type *selector_type = NULL;
2440 struct type *long_type;
2441 struct type *type;
2442
2443 struct value *ret = NULL;
2444 CORE_ADDR addr = 0;
2445
2446 value *argvec[5];
2447
2448 long_type = builtin_type (exp->gdbarch)->builtin_long;
2449 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2450
2451 if (value_as_long (target) == 0)
2452 return value_from_longest (long_type, 0);
2453
2454 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
2455 gnu_runtime = 1;
2456
2457 /* Find the method dispatch (Apple runtime) or method lookup
2458 (GNU runtime) function for Objective-C. These will be used
2459 to lookup the symbol information for the method. If we
2460 can't find any symbol information, then we'll use these to
2461 call the method, otherwise we can call the method
2462 directly. The msg_send_stret function is used in the special
2463 case of a method that returns a structure (Apple runtime
2464 only). */
2465 if (gnu_runtime)
2466 {
2467 type = selector_type;
2468
2469 type = lookup_function_type (type);
2470 type = lookup_pointer_type (type);
2471 type = lookup_function_type (type);
2472 type = lookup_pointer_type (type);
2473
2474 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
2475 msg_send_stret
2476 = find_function_in_inferior ("objc_msg_lookup", NULL);
2477
2478 msg_send = value_from_pointer (type, value_as_address (msg_send));
2479 msg_send_stret = value_from_pointer (type,
2480 value_as_address (msg_send_stret));
2481 }
2482 else
2483 {
2484 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
2485 /* Special dispatcher for methods returning structs. */
2486 msg_send_stret
2487 = find_function_in_inferior ("objc_msgSend_stret", NULL);
2488 }
2489
2490 /* Verify the target object responds to this method. The
2491 standard top-level 'Object' class uses a different name for
2492 the verification method than the non-standard, but more
2493 often used, 'NSObject' class. Make sure we check for both. */
2494
2495 responds_selector
2496 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
2497 if (responds_selector == 0)
2498 responds_selector
2499 = lookup_child_selector (exp->gdbarch, "respondsTo:");
2500
2501 if (responds_selector == 0)
2502 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
2503
2504 method_selector
2505 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
2506 if (method_selector == 0)
2507 method_selector
2508 = lookup_child_selector (exp->gdbarch, "methodFor:");
2509
2510 if (method_selector == 0)
2511 error (_("no 'methodFor:' or 'methodForSelector:' method"));
2512
2513 /* Call the verification method, to make sure that the target
2514 class implements the desired method. */
2515
2516 argvec[0] = msg_send;
2517 argvec[1] = target;
2518 argvec[2] = value_from_longest (long_type, responds_selector);
2519 argvec[3] = value_from_longest (long_type, selector);
2520 argvec[4] = 0;
2521
2522 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2523 if (gnu_runtime)
2524 {
2525 /* Function objc_msg_lookup returns a pointer. */
2526 argvec[0] = ret;
2527 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2528 }
2529 if (value_as_long (ret) == 0)
2530 error (_("Target does not respond to this message selector."));
2531
2532 /* Call "methodForSelector:" method, to get the address of a
2533 function method that implements this selector for this
2534 class. If we can find a symbol at that address, then we
2535 know the return type, parameter types etc. (that's a good
2536 thing). */
2537
2538 argvec[0] = msg_send;
2539 argvec[1] = target;
2540 argvec[2] = value_from_longest (long_type, method_selector);
2541 argvec[3] = value_from_longest (long_type, selector);
2542 argvec[4] = 0;
2543
2544 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2545 if (gnu_runtime)
2546 {
2547 argvec[0] = ret;
2548 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2549 }
2550
2551 /* ret should now be the selector. */
2552
2553 addr = value_as_long (ret);
2554 if (addr)
2555 {
2556 struct symbol *sym = NULL;
2557
2558 /* The address might point to a function descriptor;
2559 resolve it to the actual code address instead. */
2560 addr = gdbarch_convert_from_func_ptr_addr (exp->gdbarch, addr,
2561 current_top_target ());
2562
2563 /* Is it a high_level symbol? */
2564 sym = find_pc_function (addr);
2565 if (sym != NULL)
2566 method = value_of_variable (sym, 0);
2567 }
2568
2569 /* If we found a method with symbol information, check to see
2570 if it returns a struct. Otherwise assume it doesn't. */
2571
2572 if (method)
2573 {
2574 CORE_ADDR funaddr;
2575 struct type *val_type;
2576
2577 funaddr = find_function_addr (method, &val_type);
2578
2579 block_for_pc (funaddr);
2580
2581 val_type = check_typedef (val_type);
2582
2583 if ((val_type == NULL)
2584 || (val_type->code () == TYPE_CODE_ERROR))
2585 {
2586 if (expect_type != NULL)
2587 val_type = expect_type;
2588 }
2589
2590 struct_return = using_struct_return (exp->gdbarch, method,
2591 val_type);
2592 }
2593 else if (expect_type != NULL)
2594 {
2595 struct_return = using_struct_return (exp->gdbarch, NULL,
2596 check_typedef (expect_type));
2597 }
2598
2599 /* Found a function symbol. Now we will substitute its
2600 value in place of the message dispatcher (obj_msgSend),
2601 so that we call the method directly instead of thru
2602 the dispatcher. The main reason for doing this is that
2603 we can now evaluate the return value and parameter values
2604 according to their known data types, in case we need to
2605 do things like promotion, dereferencing, special handling
2606 of structs and doubles, etc.
2607
2608 We want to use the type signature of 'method', but still
2609 jump to objc_msgSend() or objc_msgSend_stret() to better
2610 mimic the behavior of the runtime. */
2611
2612 if (method)
2613 {
2614 if (value_type (method)->code () != TYPE_CODE_FUNC)
2615 error (_("method address has symbol information "
2616 "with non-function type; skipping"));
2617
2618 /* Create a function pointer of the appropriate type, and
2619 replace its value with the value of msg_send or
2620 msg_send_stret. We must use a pointer here, as
2621 msg_send and msg_send_stret are of pointer type, and
2622 the representation may be different on systems that use
2623 function descriptors. */
2624 if (struct_return)
2625 called_method
2626 = value_from_pointer (lookup_pointer_type (value_type (method)),
2627 value_as_address (msg_send_stret));
2628 else
2629 called_method
2630 = value_from_pointer (lookup_pointer_type (value_type (method)),
2631 value_as_address (msg_send));
2632 }
2633 else
2634 {
2635 if (struct_return)
2636 called_method = msg_send_stret;
2637 else
2638 called_method = msg_send;
2639 }
2640
2641 if (noside == EVAL_SKIP)
2642 return eval_skip_value (exp);
2643
2644 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2645 {
2646 /* If the return type doesn't look like a function type,
2647 call an error. This can happen if somebody tries to
2648 turn a variable into a function call. This is here
2649 because people often want to call, eg, strcmp, which
2650 gdb doesn't know is a function. If gdb isn't asked for
2651 it's opinion (ie. through "whatis"), it won't offer
2652 it. */
2653
2654 struct type *callee_type = value_type (called_method);
2655
2656 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
2657 callee_type = TYPE_TARGET_TYPE (callee_type);
2658 callee_type = TYPE_TARGET_TYPE (callee_type);
2659
2660 if (callee_type)
2661 {
2662 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
2663 return allocate_value (expect_type);
2664 else
2665 return allocate_value (callee_type);
2666 }
2667 else
2668 error (_("Expression of type other than "
2669 "\"method returning ...\" used as a method"));
2670 }
2671
2672 /* Now depending on whether we found a symbol for the method,
2673 we will either call the runtime dispatcher or the method
2674 directly. */
2675
2676 args[0] = target;
2677 args[1] = value_from_longest (long_type, selector);
2678
2679 if (gnu_runtime && (method != NULL))
2680 {
2681 /* Function objc_msg_lookup returns a pointer. */
2682 struct type *tem_type = value_type (called_method);
2683 tem_type = lookup_pointer_type (lookup_function_type (tem_type));
2684 deprecated_set_value_type (called_method, tem_type);
2685 called_method = call_function_by_hand (called_method, NULL, args);
2686 }
2687
2688 return call_function_by_hand (called_method, NULL, args);
2689 }
2690
2691 /* Helper function for MULTI_SUBSCRIPT. */
2692
2693 static struct value *
2694 eval_multi_subscript (struct type *expect_type, struct expression *exp,
2695 enum noside noside, value *arg1,
2696 gdb::array_view<value *> args)
2697 {
2698 if (noside == EVAL_SKIP)
2699 return arg1;
2700 for (value *arg2 : args)
2701 {
2702 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2))
2703 {
2704 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside);
2705 }
2706 else
2707 {
2708 arg1 = coerce_ref (arg1);
2709 struct type *type = check_typedef (value_type (arg1));
2710
2711 switch (type->code ())
2712 {
2713 case TYPE_CODE_PTR:
2714 case TYPE_CODE_ARRAY:
2715 case TYPE_CODE_STRING:
2716 arg1 = value_subscript (arg1, value_as_long (arg2));
2717 break;
2718
2719 default:
2720 if (type->name ())
2721 error (_("cannot subscript something of type `%s'"),
2722 type->name ());
2723 else
2724 error (_("cannot subscript requested type"));
2725 }
2726 }
2727 }
2728 return (arg1);
2729 }
2730
2731 namespace expr
2732 {
2733
2734 value *
2735 objc_msgcall_operation::evaluate (struct type *expect_type,
2736 struct expression *exp,
2737 enum noside noside)
2738 {
2739 enum noside sub_no_side = EVAL_NORMAL;
2740 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2741
2742 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2743 sub_no_side = EVAL_NORMAL;
2744 else
2745 sub_no_side = noside;
2746 value *target
2747 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side);
2748
2749 if (value_as_long (target) == 0)
2750 sub_no_side = EVAL_AVOID_SIDE_EFFECTS;
2751 else
2752 sub_no_side = noside;
2753 std::vector<operation_up> &args = std::get<2> (m_storage);
2754 value **argvec = XALLOCAVEC (struct value *, args.size () + 3);
2755 argvec[0] = nullptr;
2756 argvec[1] = nullptr;
2757 for (int i = 0; i < args.size (); ++i)
2758 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side);
2759 argvec[args.size () + 2] = nullptr;
2760
2761 return eval_op_objc_msgcall (expect_type, exp, noside, std::
2762 get<0> (m_storage), target,
2763 gdb::make_array_view (argvec,
2764 args.size () + 3));
2765 }
2766
2767 value *
2768 multi_subscript_operation::evaluate (struct type *expect_type,
2769 struct expression *exp,
2770 enum noside noside)
2771 {
2772 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
2773 std::vector<operation_up> &values = std::get<1> (m_storage);
2774 value **argvec = XALLOCAVEC (struct value *, values.size ());
2775 for (int ix = 0; ix < values.size (); ++ix)
2776 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside);
2777 return eval_multi_subscript (expect_type, exp, noside, arg1,
2778 gdb::make_array_view (argvec, values.size ()));
2779 }
2780
2781 value *
2782 logical_and_operation::evaluate (struct type *expect_type,
2783 struct expression *exp,
2784 enum noside noside)
2785 {
2786 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2787 if (noside == EVAL_SKIP)
2788 return eval_skip_value (exp);
2789
2790 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2791 EVAL_AVOID_SIDE_EFFECTS);
2792
2793 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2))
2794 {
2795 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2796 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside);
2797 }
2798 else
2799 {
2800 int tem = value_logical_not (arg1);
2801 if (!tem)
2802 {
2803 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2804 tem = value_logical_not (arg2);
2805 }
2806 struct type *type = language_bool_type (exp->language_defn,
2807 exp->gdbarch);
2808 return value_from_longest (type, !tem);
2809 }
2810 }
2811
2812 value *
2813 logical_or_operation::evaluate (struct type *expect_type,
2814 struct expression *exp,
2815 enum noside noside)
2816 {
2817 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2818 if (noside == EVAL_SKIP)
2819 return eval_skip_value (exp);
2820
2821 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2822 EVAL_AVOID_SIDE_EFFECTS);
2823
2824 if (binop_user_defined_p (BINOP_LOGICAL_OR, arg1, arg2))
2825 {
2826 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2827 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, OP_NULL, noside);
2828 }
2829 else
2830 {
2831 int tem = value_logical_not (arg1);
2832 if (tem)
2833 {
2834 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2835 tem = value_logical_not (arg2);
2836 }
2837
2838 struct type *type = language_bool_type (exp->language_defn,
2839 exp->gdbarch);
2840 return value_from_longest (type, !tem);
2841 }
2842 }
2843
2844 value *
2845 adl_func_operation::evaluate (struct type *expect_type,
2846 struct expression *exp,
2847 enum noside noside)
2848 {
2849 std::vector<operation_up> &arg_ops = std::get<2> (m_storage);
2850 std::vector<value *> args (arg_ops.size ());
2851 for (int i = 0; i < arg_ops.size (); ++i)
2852 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside);
2853
2854 struct symbol *symp;
2855 find_overload_match (args, std::get<0> (m_storage).c_str (),
2856 NON_METHOD,
2857 nullptr, nullptr,
2858 nullptr, &symp, nullptr, 0, noside);
2859 if (SYMBOL_TYPE (symp)->code () == TYPE_CODE_ERROR)
2860 error_unknown_type (symp->print_name ());
2861 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
2862 return evaluate_subexp_do_call (exp, noside, callee, args,
2863 nullptr, expect_type);
2864
2865 }
2866
2867 /* This function evaluates brace-initializers (in C/C++) for
2868 structure types. */
2869
2870 struct value *
2871 array_operation::evaluate_struct_tuple (struct value *struct_val,
2872 struct expression *exp,
2873 enum noside noside, int nargs)
2874 {
2875 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2876 struct type *struct_type = check_typedef (value_type (struct_val));
2877 struct type *field_type;
2878 int fieldno = -1;
2879
2880 int idx = 0;
2881 while (--nargs >= 0)
2882 {
2883 struct value *val = NULL;
2884 int bitpos, bitsize;
2885 bfd_byte *addr;
2886
2887 fieldno++;
2888 /* Skip static fields. */
2889 while (fieldno < struct_type->num_fields ()
2890 && field_is_static (&struct_type->field (fieldno)))
2891 fieldno++;
2892 if (fieldno >= struct_type->num_fields ())
2893 error (_("too many initializers"));
2894 field_type = struct_type->field (fieldno).type ();
2895 if (field_type->code () == TYPE_CODE_UNION
2896 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
2897 error (_("don't know which variant you want to set"));
2898
2899 /* Here, struct_type is the type of the inner struct,
2900 while substruct_type is the type of the inner struct.
2901 These are the same for normal structures, but a variant struct
2902 contains anonymous union fields that contain substruct fields.
2903 The value fieldno is the index of the top-level (normal or
2904 anonymous union) field in struct_field, while the value
2905 subfieldno is the index of the actual real (named inner) field
2906 in substruct_type. */
2907
2908 field_type = struct_type->field (fieldno).type ();
2909 if (val == 0)
2910 val = in_args[idx++]->evaluate (field_type, exp, noside);
2911
2912 /* Now actually set the field in struct_val. */
2913
2914 /* Assign val to field fieldno. */
2915 if (value_type (val) != field_type)
2916 val = value_cast (field_type, val);
2917
2918 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
2919 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
2920 addr = value_contents_writeable (struct_val) + bitpos / 8;
2921 if (bitsize)
2922 modify_field (struct_type, addr,
2923 value_as_long (val), bitpos % 8, bitsize);
2924 else
2925 memcpy (addr, value_contents (val),
2926 TYPE_LENGTH (value_type (val)));
2927
2928 }
2929 return struct_val;
2930 }
2931
2932 value *
2933 array_operation::evaluate (struct type *expect_type,
2934 struct expression *exp,
2935 enum noside noside)
2936 {
2937 int tem;
2938 int tem2 = std::get<0> (m_storage);
2939 int tem3 = std::get<1> (m_storage);
2940 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2941 int nargs = tem3 - tem2 + 1;
2942 struct type *type = expect_type ? check_typedef (expect_type) : nullptr;
2943
2944 if (expect_type != nullptr && noside != EVAL_SKIP
2945 && type->code () == TYPE_CODE_STRUCT)
2946 {
2947 struct value *rec = allocate_value (expect_type);
2948
2949 memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type));
2950 return evaluate_struct_tuple (rec, exp, noside, nargs);
2951 }
2952
2953 if (expect_type != nullptr && noside != EVAL_SKIP
2954 && type->code () == TYPE_CODE_ARRAY)
2955 {
2956 struct type *range_type = type->index_type ();
2957 struct type *element_type = TYPE_TARGET_TYPE (type);
2958 struct value *array = allocate_value (expect_type);
2959 int element_size = TYPE_LENGTH (check_typedef (element_type));
2960 LONGEST low_bound, high_bound, index;
2961
2962 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
2963 {
2964 low_bound = 0;
2965 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
2966 }
2967 index = low_bound;
2968 memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type));
2969 for (tem = nargs; --nargs >= 0;)
2970 {
2971 struct value *element;
2972
2973 element = in_args[index - low_bound]->evaluate (element_type,
2974 exp, noside);
2975 if (value_type (element) != element_type)
2976 element = value_cast (element_type, element);
2977 if (index > high_bound)
2978 /* To avoid memory corruption. */
2979 error (_("Too many array elements"));
2980 memcpy (value_contents_raw (array)
2981 + (index - low_bound) * element_size,
2982 value_contents (element),
2983 element_size);
2984 index++;
2985 }
2986 return array;
2987 }
2988
2989 if (expect_type != nullptr && noside != EVAL_SKIP
2990 && type->code () == TYPE_CODE_SET)
2991 {
2992 struct value *set = allocate_value (expect_type);
2993 gdb_byte *valaddr = value_contents_raw (set);
2994 struct type *element_type = type->index_type ();
2995 struct type *check_type = element_type;
2996 LONGEST low_bound, high_bound;
2997
2998 /* Get targettype of elementtype. */
2999 while (check_type->code () == TYPE_CODE_RANGE
3000 || check_type->code () == TYPE_CODE_TYPEDEF)
3001 check_type = TYPE_TARGET_TYPE (check_type);
3002
3003 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
3004 error (_("(power)set type with unknown size"));
3005 memset (valaddr, '\0', TYPE_LENGTH (type));
3006 int idx = 0;
3007 for (tem = 0; tem < nargs; tem++)
3008 {
3009 LONGEST range_low, range_high;
3010 struct type *range_low_type, *range_high_type;
3011 struct value *elem_val;
3012
3013 elem_val = in_args[idx++]->evaluate (element_type, exp, noside);
3014 range_low_type = range_high_type = value_type (elem_val);
3015 range_low = range_high = value_as_long (elem_val);
3016
3017 /* Check types of elements to avoid mixture of elements from
3018 different types. Also check if type of element is "compatible"
3019 with element type of powerset. */
3020 if (range_low_type->code () == TYPE_CODE_RANGE)
3021 range_low_type = TYPE_TARGET_TYPE (range_low_type);
3022 if (range_high_type->code () == TYPE_CODE_RANGE)
3023 range_high_type = TYPE_TARGET_TYPE (range_high_type);
3024 if ((range_low_type->code () != range_high_type->code ())
3025 || (range_low_type->code () == TYPE_CODE_ENUM
3026 && (range_low_type != range_high_type)))
3027 /* different element modes. */
3028 error (_("POWERSET tuple elements of different mode"));
3029 if ((check_type->code () != range_low_type->code ())
3030 || (check_type->code () == TYPE_CODE_ENUM
3031 && range_low_type != check_type))
3032 error (_("incompatible POWERSET tuple elements"));
3033 if (range_low > range_high)
3034 {
3035 warning (_("empty POWERSET tuple range"));
3036 continue;
3037 }
3038 if (range_low < low_bound || range_high > high_bound)
3039 error (_("POWERSET tuple element out of range"));
3040 range_low -= low_bound;
3041 range_high -= low_bound;
3042 for (; range_low <= range_high; range_low++)
3043 {
3044 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
3045
3046 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
3047 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
3048 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
3049 |= 1 << bit_index;
3050 }
3051 }
3052 return set;
3053 }
3054
3055 value **argvec = XALLOCAVEC (struct value *, nargs);
3056 for (tem = 0; tem < nargs; tem++)
3057 {
3058 /* Ensure that array expressions are coerced into pointer
3059 objects. */
3060 argvec[tem] = in_args[tem]->evaluate_with_coercion (exp, noside);
3061 }
3062 if (noside == EVAL_SKIP)
3063 return eval_skip_value (exp);
3064 return value_array (tem2, tem3, argvec);
3065 }
3066
3067 }
3068
3069 struct value *
3070 evaluate_subexp_standard (struct type *expect_type,
3071 struct expression *exp, int *pos,
3072 enum noside noside)
3073 {
3074 enum exp_opcode op;
3075 int tem, tem2, tem3;
3076 int pc, oldpos;
3077 struct value *arg1 = NULL;
3078 struct value *arg2 = NULL;
3079 struct type *type;
3080 int nargs;
3081 struct value **argvec;
3082 int ix;
3083 struct type **arg_types;
3084
3085 pc = (*pos)++;
3086 op = exp->elts[pc].opcode;
3087
3088 switch (op)
3089 {
3090 case OP_SCOPE:
3091 tem = longest_to_int (exp->elts[pc + 2].longconst);
3092 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
3093 return eval_op_scope (expect_type, exp, noside,
3094 exp->elts[pc + 1].type,
3095 &exp->elts[pc + 3].string);
3096
3097 case OP_LONG:
3098 (*pos) += 3;
3099 return value_from_longest (exp->elts[pc + 1].type,
3100 exp->elts[pc + 2].longconst);
3101
3102 case OP_FLOAT:
3103 (*pos) += 3;
3104 return value_from_contents (exp->elts[pc + 1].type,
3105 exp->elts[pc + 2].floatconst);
3106
3107 case OP_ADL_FUNC:
3108 case OP_VAR_VALUE:
3109 {
3110 (*pos) += 3;
3111 symbol *var = exp->elts[pc + 2].symbol;
3112 if (SYMBOL_TYPE (var)->code () == TYPE_CODE_ERROR)
3113 error_unknown_type (var->print_name ());
3114 if (noside != EVAL_SKIP)
3115 return evaluate_var_value (noside, exp->elts[pc + 1].block, var);
3116 else
3117 {
3118 /* Return a dummy value of the correct type when skipping, so
3119 that parent functions know what is to be skipped. */
3120 return allocate_value (SYMBOL_TYPE (var));
3121 }
3122 }
3123
3124 case OP_VAR_MSYM_VALUE:
3125 {
3126 (*pos) += 3;
3127
3128 minimal_symbol *msymbol = exp->elts[pc + 2].msymbol;
3129 return eval_op_var_msym_value (expect_type, exp, noside,
3130 pc == 0, msymbol,
3131 exp->elts[pc + 1].objfile);
3132 }
3133
3134 case OP_VAR_ENTRY_VALUE:
3135 (*pos) += 2;
3136
3137 {
3138 struct symbol *sym = exp->elts[pc + 1].symbol;
3139
3140 return eval_op_var_entry_value (expect_type, exp, noside, sym);
3141 }
3142
3143 case OP_FUNC_STATIC_VAR:
3144 tem = longest_to_int (exp->elts[pc + 1].longconst);
3145 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
3146 if (noside == EVAL_SKIP)
3147 return eval_skip_value (exp);
3148
3149 {
3150 value *func = evaluate_subexp_standard (NULL, exp, pos, noside);
3151
3152 return eval_op_func_static_var (expect_type, exp, noside, func,
3153 &exp->elts[pc + 2].string);
3154 }
3155
3156 case OP_LAST:
3157 (*pos) += 2;
3158 return
3159 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
3160
3161 case OP_REGISTER:
3162 {
3163 const char *name = &exp->elts[pc + 2].string;
3164
3165 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
3166 return eval_op_register (expect_type, exp, noside, name);
3167 }
3168 case OP_BOOL:
3169 (*pos) += 2;
3170 type = language_bool_type (exp->language_defn, exp->gdbarch);
3171 return value_from_longest (type, exp->elts[pc + 1].longconst);
3172
3173 case OP_INTERNALVAR:
3174 (*pos) += 2;
3175 return value_of_internalvar (exp->gdbarch,
3176 exp->elts[pc + 1].internalvar);
3177
3178 case OP_STRING:
3179 tem = longest_to_int (exp->elts[pc + 1].longconst);
3180 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
3181 return eval_op_string (expect_type, exp, noside, tem,
3182 &exp->elts[pc + 2].string);
3183
3184 case OP_OBJC_NSSTRING: /* Objective C Foundation Class
3185 NSString constant. */
3186 tem = longest_to_int (exp->elts[pc + 1].longconst);
3187 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
3188 if (noside == EVAL_SKIP)
3189 return eval_skip_value (exp);
3190 return value_nsstring (exp->gdbarch, &exp->elts[pc + 2].string, tem + 1);
3191
3192 case OP_ARRAY:
3193 (*pos) += 3;
3194 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
3195 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
3196 nargs = tem3 - tem2 + 1;
3197 type = expect_type ? check_typedef (expect_type) : nullptr;
3198
3199 if (expect_type != nullptr && noside != EVAL_SKIP
3200 && type->code () == TYPE_CODE_STRUCT)
3201 {
3202 struct value *rec = allocate_value (expect_type);
3203
3204 memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type));
3205 return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
3206 }
3207
3208 if (expect_type != nullptr && noside != EVAL_SKIP
3209 && type->code () == TYPE_CODE_ARRAY)
3210 {
3211 struct type *range_type = type->index_type ();
3212 struct type *element_type = TYPE_TARGET_TYPE (type);
3213 struct value *array = allocate_value (expect_type);
3214 int element_size = TYPE_LENGTH (check_typedef (element_type));
3215 LONGEST low_bound, high_bound, index;
3216
3217 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
3218 {
3219 low_bound = 0;
3220 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
3221 }
3222 index = low_bound;
3223 memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type));
3224 for (tem = nargs; --nargs >= 0;)
3225 {
3226 struct value *element;
3227
3228 element = evaluate_subexp (element_type, exp, pos, noside);
3229 if (value_type (element) != element_type)
3230 element = value_cast (element_type, element);
3231 if (index > high_bound)
3232 /* To avoid memory corruption. */
3233 error (_("Too many array elements"));
3234 memcpy (value_contents_raw (array)
3235 + (index - low_bound) * element_size,
3236 value_contents (element),
3237 element_size);
3238 index++;
3239 }
3240 return array;
3241 }
3242
3243 if (expect_type != nullptr && noside != EVAL_SKIP
3244 && type->code () == TYPE_CODE_SET)
3245 {
3246 struct value *set = allocate_value (expect_type);
3247 gdb_byte *valaddr = value_contents_raw (set);
3248 struct type *element_type = type->index_type ();
3249 struct type *check_type = element_type;
3250 LONGEST low_bound, high_bound;
3251
3252 /* Get targettype of elementtype. */
3253 while (check_type->code () == TYPE_CODE_RANGE
3254 || check_type->code () == TYPE_CODE_TYPEDEF)
3255 check_type = TYPE_TARGET_TYPE (check_type);
3256
3257 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
3258 error (_("(power)set type with unknown size"));
3259 memset (valaddr, '\0', TYPE_LENGTH (type));
3260 for (tem = 0; tem < nargs; tem++)
3261 {
3262 LONGEST range_low, range_high;
3263 struct type *range_low_type, *range_high_type;
3264 struct value *elem_val;
3265
3266 elem_val = evaluate_subexp (element_type, exp, pos, noside);
3267 range_low_type = range_high_type = value_type (elem_val);
3268 range_low = range_high = value_as_long (elem_val);
3269
3270 /* Check types of elements to avoid mixture of elements from
3271 different types. Also check if type of element is "compatible"
3272 with element type of powerset. */
3273 if (range_low_type->code () == TYPE_CODE_RANGE)
3274 range_low_type = TYPE_TARGET_TYPE (range_low_type);
3275 if (range_high_type->code () == TYPE_CODE_RANGE)
3276 range_high_type = TYPE_TARGET_TYPE (range_high_type);
3277 if ((range_low_type->code () != range_high_type->code ())
3278 || (range_low_type->code () == TYPE_CODE_ENUM
3279 && (range_low_type != range_high_type)))
3280 /* different element modes. */
3281 error (_("POWERSET tuple elements of different mode"));
3282 if ((check_type->code () != range_low_type->code ())
3283 || (check_type->code () == TYPE_CODE_ENUM
3284 && range_low_type != check_type))
3285 error (_("incompatible POWERSET tuple elements"));
3286 if (range_low > range_high)
3287 {
3288 warning (_("empty POWERSET tuple range"));
3289 continue;
3290 }
3291 if (range_low < low_bound || range_high > high_bound)
3292 error (_("POWERSET tuple element out of range"));
3293 range_low -= low_bound;
3294 range_high -= low_bound;
3295 for (; range_low <= range_high; range_low++)
3296 {
3297 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
3298
3299 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
3300 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
3301 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
3302 |= 1 << bit_index;
3303 }
3304 }
3305 return set;
3306 }
3307
3308 argvec = XALLOCAVEC (struct value *, nargs);
3309 for (tem = 0; tem < nargs; tem++)
3310 {
3311 /* Ensure that array expressions are coerced into pointer
3312 objects. */
3313 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
3314 }
3315 if (noside == EVAL_SKIP)
3316 return eval_skip_value (exp);
3317 return value_array (tem2, tem3, argvec);
3318
3319 case TERNOP_SLICE:
3320 {
3321 struct value *array = evaluate_subexp (nullptr, exp, pos, noside);
3322 struct value *low = evaluate_subexp (nullptr, exp, pos, noside);
3323 struct value *upper = evaluate_subexp (nullptr, exp, pos, noside);
3324 return eval_op_ternop (expect_type, exp, noside, array, low, upper);
3325 }
3326
3327 case TERNOP_COND:
3328 /* Skip third and second args to evaluate the first one. */
3329 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3330 if (value_logical_not (arg1))
3331 {
3332 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
3333 return evaluate_subexp (nullptr, exp, pos, noside);
3334 }
3335 else
3336 {
3337 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
3338 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
3339 return arg2;
3340 }
3341
3342 case OP_OBJC_SELECTOR:
3343 { /* Objective C @selector operator. */
3344 char *sel = &exp->elts[pc + 2].string;
3345 int len = longest_to_int (exp->elts[pc + 1].longconst);
3346
3347 (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
3348 if (sel[len] != 0)
3349 sel[len] = 0; /* Make sure it's terminated. */
3350
3351 return eval_op_objc_selector (expect_type, exp, noside, sel);
3352 }
3353
3354 case OP_OBJC_MSGCALL:
3355 { /* Objective C message (method) call. */
3356 CORE_ADDR selector = 0;
3357
3358 enum noside sub_no_side = EVAL_NORMAL;
3359
3360 struct value *target = NULL;
3361
3362 struct type *selector_type = NULL;
3363
3364 selector = exp->elts[pc + 1].longconst;
3365 nargs = exp->elts[pc + 2].longconst;
3366 argvec = XALLOCAVEC (struct value *, nargs + 3);
3367
3368 (*pos) += 3;
3369
3370 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
3371
3372 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3373 sub_no_side = EVAL_NORMAL;
3374 else
3375 sub_no_side = noside;
3376
3377 target = evaluate_subexp (selector_type, exp, pos, sub_no_side);
3378
3379 if (value_as_long (target) == 0)
3380 sub_no_side = EVAL_SKIP;
3381 else
3382 sub_no_side = noside;
3383
3384 /* Now depending on whether we found a symbol for the method,
3385 we will either call the runtime dispatcher or the method
3386 directly. */
3387
3388 argvec[0] = nullptr;
3389 argvec[1] = nullptr;
3390 /* User-supplied arguments. */
3391 for (tem = 0; tem < nargs; tem++)
3392 argvec[tem + 2] = evaluate_subexp_with_coercion (exp, pos,
3393 sub_no_side);
3394 argvec[tem + 3] = 0;
3395
3396 auto call_args = gdb::make_array_view (argvec, nargs + 3);
3397
3398 return eval_op_objc_msgcall (expect_type, exp, noside, selector,
3399 target, call_args);
3400 }
3401 break;
3402
3403 case OP_FUNCALL:
3404 return evaluate_funcall (expect_type, exp, pos, noside);
3405
3406 case OP_COMPLEX:
3407 /* We have a complex number, There should be 2 floating
3408 point numbers that compose it. */
3409 (*pos) += 2;
3410 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3411 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
3412
3413 return value_literal_complex (arg1, arg2, exp->elts[pc + 1].type);
3414
3415 case STRUCTOP_STRUCT:
3416 tem = longest_to_int (exp->elts[pc + 1].longconst);
3417 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
3418 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3419 return eval_op_structop_struct (expect_type, exp, noside, arg1,
3420 &exp->elts[pc + 2].string);
3421
3422 case STRUCTOP_PTR:
3423 tem = longest_to_int (exp->elts[pc + 1].longconst);
3424 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
3425 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3426 return eval_op_structop_ptr (expect_type, exp, noside, arg1,
3427 &exp->elts[pc + 2].string);
3428
3429 case STRUCTOP_MEMBER:
3430 case STRUCTOP_MPTR:
3431 if (op == STRUCTOP_MEMBER)
3432 arg1 = evaluate_subexp_for_address (exp, pos, noside);
3433 else
3434 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3435
3436 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
3437
3438 return eval_op_member (expect_type, exp, noside, arg1, arg2);
3439
3440 case TYPE_INSTANCE:
3441 {
3442 type_instance_flags flags
3443 = (type_instance_flag_value) longest_to_int (exp->elts[pc + 1].longconst);
3444 nargs = longest_to_int (exp->elts[pc + 2].longconst);
3445 arg_types = (struct type **) alloca (nargs * sizeof (struct type *));
3446 for (ix = 0; ix < nargs; ++ix)
3447 arg_types[ix] = exp->elts[pc + 2 + ix + 1].type;
3448
3449 fake_method fake_expect_type (flags, nargs, arg_types);
3450 *(pos) += 4 + nargs;
3451 return evaluate_subexp_standard (fake_expect_type.type (), exp, pos,
3452 noside);
3453 }
3454
3455 case BINOP_CONCAT:
3456 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
3457 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
3458 return eval_op_concat (expect_type, exp, noside, arg1, arg2);
3459
3460 case BINOP_ASSIGN:
3461 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3462 /* Special-case assignments where the left-hand-side is a
3463 convenience variable -- in these, don't bother setting an
3464 expected type. This avoids a weird case where re-assigning a
3465 string or array to an internal variable could error with "Too
3466 many array elements". */
3467 arg2 = evaluate_subexp (VALUE_LVAL (arg1) == lval_internalvar
3468 ? nullptr
3469 : value_type (arg1),
3470 exp, pos, noside);
3471
3472 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
3473 return arg1;
3474 if (binop_user_defined_p (op, arg1, arg2))
3475 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
3476 else
3477 return value_assign (arg1, arg2);
3478
3479 case BINOP_ASSIGN_MODIFY:
3480 (*pos) += 2;
3481 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3482 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
3483 op = exp->elts[pc + 1].opcode;
3484 return eval_binop_assign_modify (expect_type, exp, noside, op,
3485 arg1, arg2);
3486
3487 case BINOP_ADD:
3488 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
3489 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
3490 return eval_op_add (expect_type, exp, noside, arg1, arg2);
3491
3492 case BINOP_SUB:
3493 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
3494 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
3495 return eval_op_sub (expect_type, exp, noside, arg1, arg2);
3496
3497 case BINOP_EXP:
3498 case BINOP_MUL:
3499 case BINOP_DIV:
3500 case BINOP_INTDIV:
3501 case BINOP_REM:
3502 case BINOP_MOD:
3503 case BINOP_LSH:
3504 case BINOP_RSH:
3505 case BINOP_BITWISE_AND:
3506 case BINOP_BITWISE_IOR:
3507 case BINOP_BITWISE_XOR:
3508 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3509 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
3510 return eval_op_binary (expect_type, exp, noside, op, arg1, arg2);
3511
3512 case BINOP_SUBSCRIPT:
3513 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3514 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
3515 return eval_op_subscript (expect_type, exp, noside, op, arg1, arg2);
3516
3517 case MULTI_SUBSCRIPT:
3518 (*pos) += 2;
3519 nargs = longest_to_int (exp->elts[pc + 1].longconst);
3520 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
3521 argvec = XALLOCAVEC (struct value *, nargs);
3522 for (ix = 0; ix < nargs; ++ix)
3523 argvec[ix] = evaluate_subexp_with_coercion (exp, pos, noside);
3524 return eval_multi_subscript (expect_type, exp, noside, arg1,
3525 gdb::make_array_view (argvec, nargs));
3526
3527 case BINOP_LOGICAL_AND:
3528 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3529 if (noside == EVAL_SKIP)
3530 {
3531 evaluate_subexp (nullptr, exp, pos, noside);
3532 return eval_skip_value (exp);
3533 }
3534
3535 oldpos = *pos;
3536 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3537 *pos = oldpos;
3538
3539 if (binop_user_defined_p (op, arg1, arg2))
3540 {
3541 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
3542 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
3543 }
3544 else
3545 {
3546 tem = value_logical_not (arg1);
3547 arg2
3548 = evaluate_subexp (nullptr, exp, pos, (tem ? EVAL_SKIP : noside));
3549 type = language_bool_type (exp->language_defn, exp->gdbarch);
3550 return value_from_longest (type,
3551 (LONGEST) (!tem && !value_logical_not (arg2)));
3552 }
3553
3554 case BINOP_LOGICAL_OR:
3555 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3556 if (noside == EVAL_SKIP)
3557 {
3558 evaluate_subexp (nullptr, exp, pos, noside);
3559 return eval_skip_value (exp);
3560 }
3561
3562 oldpos = *pos;
3563 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3564 *pos = oldpos;
3565
3566 if (binop_user_defined_p (op, arg1, arg2))
3567 {
3568 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
3569 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
3570 }
3571 else
3572 {
3573 tem = value_logical_not (arg1);
3574 arg2
3575 = evaluate_subexp (nullptr, exp, pos, (!tem ? EVAL_SKIP : noside));
3576 type = language_bool_type (exp->language_defn, exp->gdbarch);
3577 return value_from_longest (type,
3578 (LONGEST) (!tem || !value_logical_not (arg2)));
3579 }
3580
3581 case BINOP_EQUAL:
3582 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3583 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
3584 return eval_op_equal (expect_type, exp, noside, op, arg1, arg2);
3585
3586 case BINOP_NOTEQUAL:
3587 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3588 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
3589 return eval_op_notequal (expect_type, exp, noside, op, arg1, arg2);
3590
3591 case BINOP_LESS:
3592 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3593 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
3594 return eval_op_less (expect_type, exp, noside, op, arg1, arg2);
3595
3596 case BINOP_GTR:
3597 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3598 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
3599 return eval_op_gtr (expect_type, exp, noside, op, arg1, arg2);
3600
3601 case BINOP_GEQ:
3602 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3603 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
3604 return eval_op_geq (expect_type, exp, noside, op, arg1, arg2);
3605
3606 case BINOP_LEQ:
3607 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3608 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
3609 return eval_op_leq (expect_type, exp, noside, op, arg1, arg2);
3610
3611 case BINOP_REPEAT:
3612 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3613 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
3614 return eval_op_repeat (expect_type, exp, noside, op, arg1, arg2);
3615
3616 case BINOP_COMMA:
3617 evaluate_subexp (nullptr, exp, pos, noside);
3618 return evaluate_subexp (nullptr, exp, pos, noside);
3619
3620 case UNOP_PLUS:
3621 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3622 return eval_op_plus (expect_type, exp, noside, op, arg1);
3623
3624 case UNOP_NEG:
3625 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3626 return eval_op_neg (expect_type, exp, noside, op, arg1);
3627
3628 case UNOP_COMPLEMENT:
3629 /* C++: check for and handle destructor names. */
3630
3631 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3632 return eval_op_complement (expect_type, exp, noside, op, arg1);
3633
3634 case UNOP_LOGICAL_NOT:
3635 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
3636 return eval_op_lognot (expect_type, exp, noside, op, arg1);
3637
3638 case UNOP_IND:
3639 if (expect_type && expect_type->code () == TYPE_CODE_PTR)
3640 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
3641 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
3642 return eval_op_ind (expect_type, exp, noside, arg1);
3643
3644 case UNOP_ADDR:
3645 /* C++: check for and handle pointer to members. */
3646
3647 if (noside == EVAL_SKIP)
3648 {
3649 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
3650 return eval_skip_value (exp);
3651 }
3652 else
3653 return evaluate_subexp_for_address (exp, pos, noside);
3654
3655 case UNOP_SIZEOF:
3656 if (noside == EVAL_SKIP)
3657 {
3658 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
3659 return eval_skip_value (exp);
3660 }
3661 return evaluate_subexp_for_sizeof (exp, pos, noside);
3662
3663 case UNOP_ALIGNOF:
3664 arg1 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3665 return eval_op_alignof (expect_type, exp, noside, arg1);
3666
3667 case UNOP_CAST:
3668 (*pos) += 2;
3669 type = exp->elts[pc + 1].type;
3670 return evaluate_subexp_for_cast (exp, pos, noside, type);
3671
3672 case UNOP_CAST_TYPE:
3673 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3674 type = value_type (arg1);
3675 return evaluate_subexp_for_cast (exp, pos, noside, type);
3676
3677 case UNOP_DYNAMIC_CAST:
3678 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3679 type = value_type (arg1);
3680 arg1 = evaluate_subexp (type, exp, pos, noside);
3681 if (noside == EVAL_SKIP)
3682 return eval_skip_value (exp);
3683 return value_dynamic_cast (type, arg1);
3684
3685 case UNOP_REINTERPRET_CAST:
3686 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3687 type = value_type (arg1);
3688 arg1 = evaluate_subexp (type, exp, pos, noside);
3689 if (noside == EVAL_SKIP)
3690 return eval_skip_value (exp);
3691 return value_reinterpret_cast (type, arg1);
3692
3693 case UNOP_MEMVAL:
3694 (*pos) += 2;
3695 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
3696 return eval_op_memval (expect_type, exp, noside, arg1,
3697 exp->elts[pc + 1].type);
3698
3699 case UNOP_MEMVAL_TYPE:
3700 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3701 type = value_type (arg1);
3702 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
3703 return eval_op_memval (expect_type, exp, noside, arg1, type);
3704
3705 case UNOP_PREINCREMENT:
3706 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
3707 return eval_op_preinc (expect_type, exp, noside, op, arg1);
3708
3709 case UNOP_PREDECREMENT:
3710 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
3711 return eval_op_predec (expect_type, exp, noside, op, arg1);
3712
3713 case UNOP_POSTINCREMENT:
3714 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
3715 return eval_op_postinc (expect_type, exp, noside, op, arg1);
3716
3717 case UNOP_POSTDECREMENT:
3718 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
3719 return eval_op_postdec (expect_type, exp, noside, op, arg1);
3720
3721 case OP_THIS:
3722 (*pos) += 1;
3723 return value_of_this (exp->language_defn);
3724
3725 case OP_TYPE:
3726 /* The value is not supposed to be used. This is here to make it
3727 easier to accommodate expressions that contain types. */
3728 (*pos) += 2;
3729 return eval_op_type (expect_type, exp, noside, exp->elts[pc + 1].type);
3730
3731 case OP_TYPEOF:
3732 case OP_DECLTYPE:
3733 if (noside == EVAL_SKIP)
3734 {
3735 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
3736 return eval_skip_value (exp);
3737 }
3738 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
3739 {
3740 enum exp_opcode sub_op = exp->elts[*pos].opcode;
3741 struct value *result;
3742
3743 result = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3744
3745 /* 'decltype' has special semantics for lvalues. */
3746 if (op == OP_DECLTYPE
3747 && (sub_op == BINOP_SUBSCRIPT
3748 || sub_op == STRUCTOP_MEMBER
3749 || sub_op == STRUCTOP_MPTR
3750 || sub_op == UNOP_IND
3751 || sub_op == STRUCTOP_STRUCT
3752 || sub_op == STRUCTOP_PTR
3753 || sub_op == OP_SCOPE))
3754 {
3755 type = value_type (result);
3756
3757 if (!TYPE_IS_REFERENCE (type))
3758 {
3759 type = lookup_lvalue_reference_type (type);
3760 result = allocate_value (type);
3761 }
3762 }
3763
3764 return result;
3765 }
3766 else
3767 error (_("Attempt to use a type as an expression"));
3768
3769 case OP_TYPEID:
3770 {
3771 struct value *result;
3772 enum exp_opcode sub_op = exp->elts[*pos].opcode;
3773
3774 if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
3775 result = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3776 else
3777 result = evaluate_subexp (nullptr, exp, pos, noside);
3778
3779 if (noside != EVAL_NORMAL)
3780 return allocate_value (cplus_typeid_type (exp->gdbarch));
3781
3782 return cplus_typeid (result);
3783 }
3784
3785 default:
3786 /* Removing this case and compiling with gcc -Wall reveals that
3787 a lot of cases are hitting this case. Some of these should
3788 probably be removed from expression.h; others are legitimate
3789 expressions which are (apparently) not fully implemented.
3790
3791 If there are any cases landing here which mean a user error,
3792 then they should be separate cases, with more descriptive
3793 error messages. */
3794
3795 error (_("GDB does not (yet) know how to "
3796 "evaluate that kind of expression"));
3797 }
3798
3799 gdb_assert_not_reached ("missed return?");
3800 }
3801 \f
3802 /* Helper for evaluate_subexp_for_address. */
3803
3804 static value *
3805 evaluate_subexp_for_address_base (struct expression *exp, enum noside noside,
3806 value *x)
3807 {
3808 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3809 {
3810 struct type *type = check_typedef (value_type (x));
3811
3812 if (TYPE_IS_REFERENCE (type))
3813 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
3814 not_lval);
3815 else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
3816 return value_zero (lookup_pointer_type (value_type (x)),
3817 not_lval);
3818 else
3819 error (_("Attempt to take address of "
3820 "value not located in memory."));
3821 }
3822 return value_addr (x);
3823 }
3824
3825 /* Evaluate a subexpression of EXP, at index *POS,
3826 and return the address of that subexpression.
3827 Advance *POS over the subexpression.
3828 If the subexpression isn't an lvalue, get an error.
3829 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
3830 then only the type of the result need be correct. */
3831
3832 static struct value *
3833 evaluate_subexp_for_address (struct expression *exp, int *pos,
3834 enum noside noside)
3835 {
3836 enum exp_opcode op;
3837 int pc;
3838 struct symbol *var;
3839 struct value *x;
3840 int tem;
3841
3842 pc = (*pos);
3843 op = exp->elts[pc].opcode;
3844
3845 switch (op)
3846 {
3847 case UNOP_IND:
3848 (*pos)++;
3849 x = evaluate_subexp (nullptr, exp, pos, noside);
3850
3851 /* We can't optimize out "&*" if there's a user-defined operator*. */
3852 if (unop_user_defined_p (op, x))
3853 {
3854 x = value_x_unop (x, op, noside);
3855 goto default_case_after_eval;
3856 }
3857
3858 return coerce_array (x);
3859
3860 case UNOP_MEMVAL:
3861 (*pos) += 3;
3862 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
3863 evaluate_subexp (nullptr, exp, pos, noside));
3864
3865 case UNOP_MEMVAL_TYPE:
3866 {
3867 struct type *type;
3868
3869 (*pos) += 1;
3870 x = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3871 type = value_type (x);
3872 return value_cast (lookup_pointer_type (type),
3873 evaluate_subexp (nullptr, exp, pos, noside));
3874 }
3875
3876 case OP_VAR_VALUE:
3877 var = exp->elts[pc + 2].symbol;
3878
3879 /* C++: The "address" of a reference should yield the address
3880 * of the object pointed to. Let value_addr() deal with it. */
3881 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var)))
3882 goto default_case;
3883
3884 (*pos) += 4;
3885 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3886 {
3887 struct type *type =
3888 lookup_pointer_type (SYMBOL_TYPE (var));
3889 enum address_class sym_class = SYMBOL_CLASS (var);
3890
3891 if (sym_class == LOC_CONST
3892 || sym_class == LOC_CONST_BYTES
3893 || sym_class == LOC_REGISTER)
3894 error (_("Attempt to take address of register or constant."));
3895
3896 return
3897 value_zero (type, not_lval);
3898 }
3899 else
3900 return address_of_variable (var, exp->elts[pc + 1].block);
3901
3902 case OP_VAR_MSYM_VALUE:
3903 {
3904 (*pos) += 4;
3905
3906 value *val = evaluate_var_msym_value (noside,
3907 exp->elts[pc + 1].objfile,
3908 exp->elts[pc + 2].msymbol);
3909 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3910 {
3911 struct type *type = lookup_pointer_type (value_type (val));
3912 return value_zero (type, not_lval);
3913 }
3914 else
3915 return value_addr (val);
3916 }
3917
3918 case OP_SCOPE:
3919 tem = longest_to_int (exp->elts[pc + 2].longconst);
3920 (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1);
3921 x = value_aggregate_elt (exp->elts[pc + 1].type,
3922 &exp->elts[pc + 3].string,
3923 NULL, 1, noside);
3924 if (x == NULL)
3925 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
3926 return x;
3927
3928 default:
3929 default_case:
3930 x = evaluate_subexp (nullptr, exp, pos, noside);
3931 default_case_after_eval:
3932 return evaluate_subexp_for_address_base (exp, noside, x);
3933 }
3934 }
3935
3936 namespace expr
3937 {
3938
3939 value *
3940 operation::evaluate_for_cast (struct type *expect_type,
3941 struct expression *exp,
3942 enum noside noside)
3943 {
3944 value *val = evaluate (expect_type, exp, noside);
3945 if (noside == EVAL_SKIP)
3946 return eval_skip_value (exp);
3947 return value_cast (expect_type, val);
3948 }
3949
3950 value *
3951 operation::evaluate_for_address (struct expression *exp, enum noside noside)
3952 {
3953 value *val = evaluate (nullptr, exp, noside);
3954 return evaluate_subexp_for_address_base (exp, noside, val);
3955 }
3956
3957 value *
3958 scope_operation::evaluate_for_address (struct expression *exp,
3959 enum noside noside)
3960 {
3961 value *x = value_aggregate_elt (std::get<0> (m_storage),
3962 std::get<1> (m_storage).c_str (),
3963 NULL, 1, noside);
3964 if (x == NULL)
3965 error (_("There is no field named %s"), std::get<1> (m_storage).c_str ());
3966 return x;
3967 }
3968
3969 value *
3970 unop_ind_base_operation::evaluate_for_address (struct expression *exp,
3971 enum noside noside)
3972 {
3973 value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
3974
3975 /* We can't optimize out "&*" if there's a user-defined operator*. */
3976 if (unop_user_defined_p (UNOP_IND, x))
3977 {
3978 x = value_x_unop (x, UNOP_IND, noside);
3979 return evaluate_subexp_for_address_base (exp, noside, x);
3980 }
3981
3982 return coerce_array (x);
3983 }
3984
3985 value *
3986 var_msym_value_operation::evaluate_for_address (struct expression *exp,
3987 enum noside noside)
3988 {
3989 value *val = evaluate_var_msym_value (noside,
3990 std::get<1> (m_storage),
3991 std::get<0> (m_storage));
3992 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3993 {
3994 struct type *type = lookup_pointer_type (value_type (val));
3995 return value_zero (type, not_lval);
3996 }
3997 else
3998 return value_addr (val);
3999 }
4000
4001 value *
4002 unop_memval_operation::evaluate_for_address (struct expression *exp,
4003 enum noside noside)
4004 {
4005 return value_cast (lookup_pointer_type (std::get<1> (m_storage)),
4006 std::get<0> (m_storage)->evaluate (nullptr, exp, noside));
4007 }
4008
4009 value *
4010 unop_memval_type_operation::evaluate_for_address (struct expression *exp,
4011 enum noside noside)
4012 {
4013 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
4014 EVAL_AVOID_SIDE_EFFECTS);
4015 struct type *type = value_type (typeval);
4016 return value_cast (lookup_pointer_type (type),
4017 std::get<1> (m_storage)->evaluate (nullptr, exp, noside));
4018 }
4019
4020 }
4021
4022 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
4023 When used in contexts where arrays will be coerced anyway, this is
4024 equivalent to `evaluate_subexp' but much faster because it avoids
4025 actually fetching array contents (perhaps obsolete now that we have
4026 value_lazy()).
4027
4028 Note that we currently only do the coercion for C expressions, where
4029 arrays are zero based and the coercion is correct. For other languages,
4030 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
4031 to decide if coercion is appropriate. */
4032
4033 struct value *
4034 evaluate_subexp_with_coercion (struct expression *exp,
4035 int *pos, enum noside noside)
4036 {
4037 enum exp_opcode op;
4038 int pc;
4039 struct value *val;
4040 struct symbol *var;
4041 struct type *type;
4042
4043 pc = (*pos);
4044 op = exp->elts[pc].opcode;
4045
4046 switch (op)
4047 {
4048 case OP_VAR_VALUE:
4049 var = exp->elts[pc + 2].symbol;
4050 type = check_typedef (SYMBOL_TYPE (var));
4051 if (type->code () == TYPE_CODE_ARRAY
4052 && !type->is_vector ()
4053 && CAST_IS_CONVERSION (exp->language_defn))
4054 {
4055 (*pos) += 4;
4056 val = address_of_variable (var, exp->elts[pc + 1].block);
4057 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
4058 val);
4059 }
4060 /* FALLTHROUGH */
4061
4062 default:
4063 return evaluate_subexp (nullptr, exp, pos, noside);
4064 }
4065 }
4066
4067 namespace expr
4068 {
4069
4070 value *
4071 var_value_operation::evaluate_for_address (struct expression *exp,
4072 enum noside noside)
4073 {
4074 symbol *var = std::get<0> (m_storage);
4075
4076 /* C++: The "address" of a reference should yield the address
4077 * of the object pointed to. Let value_addr() deal with it. */
4078 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var)))
4079 return operation::evaluate_for_address (exp, noside);
4080
4081 if (noside == EVAL_AVOID_SIDE_EFFECTS)
4082 {
4083 struct type *type = lookup_pointer_type (SYMBOL_TYPE (var));
4084 enum address_class sym_class = SYMBOL_CLASS (var);
4085
4086 if (sym_class == LOC_CONST
4087 || sym_class == LOC_CONST_BYTES
4088 || sym_class == LOC_REGISTER)
4089 error (_("Attempt to take address of register or constant."));
4090
4091 return value_zero (type, not_lval);
4092 }
4093 else
4094 return address_of_variable (var, std::get<1> (m_storage));
4095 }
4096
4097 value *
4098 var_value_operation::evaluate_with_coercion (struct expression *exp,
4099 enum noside noside)
4100 {
4101 struct symbol *var = std::get<0> (m_storage);
4102 struct type *type = check_typedef (SYMBOL_TYPE (var));
4103 if (type->code () == TYPE_CODE_ARRAY
4104 && !type->is_vector ()
4105 && CAST_IS_CONVERSION (exp->language_defn))
4106 {
4107 struct value *val = address_of_variable (var, std::get<1> (m_storage));
4108 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)), val);
4109 }
4110 return evaluate (nullptr, exp, noside);
4111 }
4112
4113 }
4114
4115 /* Helper function for evaluating the size of a type. */
4116
4117 static value *
4118 evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type)
4119 {
4120 /* FIXME: This should be size_t. */
4121 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
4122 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
4123 "When applied to a reference or a reference type, the result is
4124 the size of the referenced type." */
4125 type = check_typedef (type);
4126 if (exp->language_defn->la_language == language_cplus
4127 && (TYPE_IS_REFERENCE (type)))
4128 type = check_typedef (TYPE_TARGET_TYPE (type));
4129 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
4130 }
4131
4132 /* Evaluate a subexpression of EXP, at index *POS,
4133 and return a value for the size of that subexpression.
4134 Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
4135 we allow side-effects on the operand if its type is a variable
4136 length array. */
4137
4138 static struct value *
4139 evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
4140 enum noside noside)
4141 {
4142 /* FIXME: This should be size_t. */
4143 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
4144 enum exp_opcode op;
4145 int pc;
4146 struct type *type;
4147 struct value *val;
4148
4149 pc = (*pos);
4150 op = exp->elts[pc].opcode;
4151
4152 switch (op)
4153 {
4154 /* This case is handled specially
4155 so that we avoid creating a value for the result type.
4156 If the result type is very big, it's desirable not to
4157 create a value unnecessarily. */
4158 case UNOP_IND:
4159 (*pos)++;
4160 val = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
4161 type = check_typedef (value_type (val));
4162 if (type->code () != TYPE_CODE_PTR
4163 && !TYPE_IS_REFERENCE (type)
4164 && type->code () != TYPE_CODE_ARRAY)
4165 error (_("Attempt to take contents of a non-pointer value."));
4166 type = TYPE_TARGET_TYPE (type);
4167 if (is_dynamic_type (type))
4168 type = value_type (value_ind (val));
4169 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
4170
4171 case UNOP_MEMVAL:
4172 (*pos) += 3;
4173 type = exp->elts[pc + 1].type;
4174 break;
4175
4176 case UNOP_MEMVAL_TYPE:
4177 (*pos) += 1;
4178 val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
4179 type = value_type (val);
4180 break;
4181
4182 case OP_VAR_VALUE:
4183 type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
4184 if (is_dynamic_type (type))
4185 {
4186 val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL);
4187 type = value_type (val);
4188 if (type->code () == TYPE_CODE_ARRAY)
4189 {
4190 if (type_not_allocated (type) || type_not_associated (type))
4191 return value_zero (size_type, not_lval);
4192 else if (is_dynamic_type (type->index_type ())
4193 && type->bounds ()->high.kind () == PROP_UNDEFINED)
4194 return allocate_optimized_out_value (size_type);
4195 }
4196 }
4197 else
4198 (*pos) += 4;
4199 break;
4200
4201 case OP_VAR_MSYM_VALUE:
4202 {
4203 (*pos) += 4;
4204
4205 minimal_symbol *msymbol = exp->elts[pc + 2].msymbol;
4206 value *mval = evaluate_var_msym_value (noside,
4207 exp->elts[pc + 1].objfile,
4208 msymbol);
4209
4210 type = value_type (mval);
4211 if (type->code () == TYPE_CODE_ERROR)
4212 error_unknown_type (msymbol->print_name ());
4213
4214 return value_from_longest (size_type, TYPE_LENGTH (type));
4215 }
4216 break;
4217
4218 /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
4219 type of the subscript is a variable length array type. In this case we
4220 must re-evaluate the right hand side of the subscription to allow
4221 side-effects. */
4222 case BINOP_SUBSCRIPT:
4223 if (noside == EVAL_NORMAL)
4224 {
4225 int npc = (*pos) + 1;
4226
4227 val = evaluate_subexp (nullptr, exp, &npc, EVAL_AVOID_SIDE_EFFECTS);
4228 type = check_typedef (value_type (val));
4229 if (type->code () == TYPE_CODE_ARRAY)
4230 {
4231 type = check_typedef (TYPE_TARGET_TYPE (type));
4232 if (type->code () == TYPE_CODE_ARRAY)
4233 {
4234 type = type->index_type ();
4235 /* Only re-evaluate the right hand side if the resulting type
4236 is a variable length type. */
4237 if (type->bounds ()->flag_bound_evaluated)
4238 {
4239 val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL);
4240 return value_from_longest
4241 (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
4242 }
4243 }
4244 }
4245 }
4246
4247 /* Fall through. */
4248
4249 default:
4250 val = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
4251 type = value_type (val);
4252 break;
4253 }
4254
4255 return evaluate_subexp_for_sizeof_base (exp, type);
4256 }
4257
4258 namespace expr
4259 {
4260
4261 value *
4262 operation::evaluate_for_sizeof (struct expression *exp, enum noside noside)
4263 {
4264 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
4265 return evaluate_subexp_for_sizeof_base (exp, value_type (val));
4266 }
4267
4268 value *
4269 var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
4270 enum noside noside)
4271
4272 {
4273 minimal_symbol *msymbol = std::get<0> (m_storage);
4274 value *mval = evaluate_var_msym_value (noside,
4275 std::get<1> (m_storage),
4276 msymbol);
4277
4278 struct type *type = value_type (mval);
4279 if (type->code () == TYPE_CODE_ERROR)
4280 error_unknown_type (msymbol->print_name ());
4281
4282 /* FIXME: This should be size_t. */
4283 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
4284 return value_from_longest (size_type, TYPE_LENGTH (type));
4285 }
4286
4287 value *
4288 subscript_operation::evaluate_for_sizeof (struct expression *exp,
4289 enum noside noside)
4290 {
4291 if (noside == EVAL_NORMAL)
4292 {
4293 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
4294 EVAL_AVOID_SIDE_EFFECTS);
4295 struct type *type = check_typedef (value_type (val));
4296 if (type->code () == TYPE_CODE_ARRAY)
4297 {
4298 type = check_typedef (TYPE_TARGET_TYPE (type));
4299 if (type->code () == TYPE_CODE_ARRAY)
4300 {
4301 type = type->index_type ();
4302 /* Only re-evaluate the right hand side if the resulting type
4303 is a variable length type. */
4304 if (type->bounds ()->flag_bound_evaluated)
4305 {
4306 val = evaluate (nullptr, exp, EVAL_NORMAL);
4307 /* FIXME: This should be size_t. */
4308 struct type *size_type
4309 = builtin_type (exp->gdbarch)->builtin_int;
4310 return value_from_longest
4311 (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
4312 }
4313 }
4314 }
4315 }
4316
4317 return operation::evaluate_for_sizeof (exp, noside);
4318 }
4319
4320 value *
4321 unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
4322 enum noside noside)
4323 {
4324 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
4325 EVAL_AVOID_SIDE_EFFECTS);
4326 struct type *type = check_typedef (value_type (val));
4327 if (type->code () != TYPE_CODE_PTR
4328 && !TYPE_IS_REFERENCE (type)
4329 && type->code () != TYPE_CODE_ARRAY)
4330 error (_("Attempt to take contents of a non-pointer value."));
4331 type = TYPE_TARGET_TYPE (type);
4332 if (is_dynamic_type (type))
4333 type = value_type (value_ind (val));
4334 /* FIXME: This should be size_t. */
4335 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
4336 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
4337 }
4338
4339 value *
4340 unop_memval_operation::evaluate_for_sizeof (struct expression *exp,
4341 enum noside noside)
4342 {
4343 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage));
4344 }
4345
4346 value *
4347 unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
4348 enum noside noside)
4349 {
4350 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
4351 EVAL_AVOID_SIDE_EFFECTS);
4352 return evaluate_subexp_for_sizeof_base (exp, value_type (typeval));
4353 }
4354
4355 value *
4356 var_value_operation::evaluate_for_sizeof (struct expression *exp,
4357 enum noside noside)
4358 {
4359 struct type *type = SYMBOL_TYPE (std::get<0> (m_storage));
4360 if (is_dynamic_type (type))
4361 {
4362 value *val = evaluate (nullptr, exp, EVAL_NORMAL);
4363 type = value_type (val);
4364 if (type->code () == TYPE_CODE_ARRAY)
4365 {
4366 /* FIXME: This should be size_t. */
4367 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
4368 if (type_not_allocated (type) || type_not_associated (type))
4369 return value_zero (size_type, not_lval);
4370 else if (is_dynamic_type (type->index_type ())
4371 && type->bounds ()->high.kind () == PROP_UNDEFINED)
4372 return allocate_optimized_out_value (size_type);
4373 }
4374 }
4375 return evaluate_subexp_for_sizeof_base (exp, type);
4376 }
4377
4378 }
4379
4380 /* Evaluate a subexpression of EXP, at index *POS, and return a value
4381 for that subexpression cast to TO_TYPE. Advance *POS over the
4382 subexpression. */
4383
4384 static value *
4385 evaluate_subexp_for_cast (expression *exp, int *pos,
4386 enum noside noside,
4387 struct type *to_type)
4388 {
4389 int pc = *pos;
4390
4391 /* Don't let symbols be evaluated with evaluate_subexp because that
4392 throws an "unknown type" error for no-debug data symbols.
4393 Instead, we want the cast to reinterpret the symbol. */
4394 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE
4395 || exp->elts[pc].opcode == OP_VAR_VALUE)
4396 {
4397 (*pos) += 4;
4398
4399 value *val;
4400 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE)
4401 {
4402 if (noside == EVAL_AVOID_SIDE_EFFECTS)
4403 return value_zero (to_type, not_lval);
4404
4405 val = evaluate_var_msym_value (noside,
4406 exp->elts[pc + 1].objfile,
4407 exp->elts[pc + 2].msymbol);
4408 }
4409 else
4410 val = evaluate_var_value (noside,
4411 exp->elts[pc + 1].block,
4412 exp->elts[pc + 2].symbol);
4413
4414 if (noside == EVAL_SKIP)
4415 return eval_skip_value (exp);
4416
4417 val = value_cast (to_type, val);
4418
4419 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
4420 if (VALUE_LVAL (val) == lval_memory)
4421 {
4422 if (value_lazy (val))
4423 value_fetch_lazy (val);
4424 VALUE_LVAL (val) = not_lval;
4425 }
4426 return val;
4427 }
4428
4429 value *val = evaluate_subexp (to_type, exp, pos, noside);
4430 if (noside == EVAL_SKIP)
4431 return eval_skip_value (exp);
4432 return value_cast (to_type, val);
4433 }
4434
4435 namespace expr
4436 {
4437
4438 value *
4439 var_msym_value_operation::evaluate_for_cast (struct type *to_type,
4440 struct expression *exp,
4441 enum noside noside)
4442 {
4443 if (noside == EVAL_AVOID_SIDE_EFFECTS)
4444 return value_zero (to_type, not_lval);
4445
4446 value *val = evaluate_var_msym_value (noside,
4447 std::get<1> (m_storage),
4448 std::get<0> (m_storage));
4449
4450 if (noside == EVAL_SKIP)
4451 return eval_skip_value (exp);
4452
4453 val = value_cast (to_type, val);
4454
4455 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
4456 if (VALUE_LVAL (val) == lval_memory)
4457 {
4458 if (value_lazy (val))
4459 value_fetch_lazy (val);
4460 VALUE_LVAL (val) = not_lval;
4461 }
4462 return val;
4463 }
4464
4465 value *
4466 var_value_operation::evaluate_for_cast (struct type *to_type,
4467 struct expression *exp,
4468 enum noside noside)
4469 {
4470 value *val = evaluate_var_value (noside,
4471 std::get<1> (m_storage),
4472 std::get<0> (m_storage));
4473
4474 if (noside == EVAL_SKIP)
4475 return eval_skip_value (exp);
4476
4477 val = value_cast (to_type, val);
4478
4479 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
4480 if (VALUE_LVAL (val) == lval_memory)
4481 {
4482 if (value_lazy (val))
4483 value_fetch_lazy (val);
4484 VALUE_LVAL (val) = not_lval;
4485 }
4486 return val;
4487 }
4488
4489 }
4490
4491 /* Parse a type expression in the string [P..P+LENGTH). */
4492
4493 struct type *
4494 parse_and_eval_type (const char *p, int length)
4495 {
4496 char *tmp = (char *) alloca (length + 4);
4497
4498 tmp[0] = '(';
4499 memcpy (tmp + 1, p, length);
4500 tmp[length + 1] = ')';
4501 tmp[length + 2] = '0';
4502 tmp[length + 3] = '\0';
4503 expression_up expr = parse_expression (tmp);
4504 if (expr->first_opcode () != UNOP_CAST)
4505 error (_("Internal error in eval_type."));
4506
4507 if (expr->op != nullptr)
4508 {
4509 expr::unop_cast_operation *op
4510 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ());
4511 return op->get_type ();
4512 }
4513
4514 return expr->elts[1].type;
4515 }
This page took 0.124138 seconds and 4 git commands to generate.