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