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