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