Update copyright year range in all GDB files
[deliverable/binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "value.h"
24 #include "expression.h"
25 #include "target.h"
26 #include "frame.h"
27 #include "gdbthread.h"
28 #include "language.h" /* For CAST_IS_CONVERSION. */
29 #include "cp-abi.h"
30 #include "infcall.h"
31 #include "objc-lang.h"
32 #include "block.h"
33 #include "parser-defs.h"
34 #include "cp-support.h"
35 #include "ui-out.h"
36 #include "regcache.h"
37 #include "user-regs.h"
38 #include "valprint.h"
39 #include "gdb_obstack.h"
40 #include "objfiles.h"
41 #include "typeprint.h"
42 #include <ctype.h>
43
44 /* Prototypes for local functions. */
45
46 static struct value *evaluate_subexp_for_sizeof (struct expression *, int *,
47 enum noside);
48
49 static struct value *evaluate_subexp_for_address (struct expression *,
50 int *, enum noside);
51
52 static value *evaluate_subexp_for_cast (expression *exp, int *pos,
53 enum noside noside,
54 struct type *type);
55
56 static struct value *evaluate_struct_tuple (struct value *,
57 struct expression *, int *,
58 enum noside, int);
59
60 struct value *
61 evaluate_subexp (struct type *expect_type, struct expression *exp,
62 int *pos, enum noside noside)
63 {
64 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 argvec = XALLOCAVEC (struct value *, nargs);
2146 for (ix = 0; ix < nargs; ++ix)
2147 argvec[ix] = evaluate_subexp_with_coercion (exp, pos, noside);
2148 if (noside == EVAL_SKIP)
2149 return arg1;
2150 for (ix = 0; ix < nargs; ++ix)
2151 {
2152 arg2 = argvec[ix];
2153
2154 if (binop_user_defined_p (op, arg1, arg2))
2155 {
2156 arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside);
2157 }
2158 else
2159 {
2160 arg1 = coerce_ref (arg1);
2161 type = check_typedef (value_type (arg1));
2162
2163 switch (type->code ())
2164 {
2165 case TYPE_CODE_PTR:
2166 case TYPE_CODE_ARRAY:
2167 case TYPE_CODE_STRING:
2168 arg1 = value_subscript (arg1, value_as_long (arg2));
2169 break;
2170
2171 default:
2172 if (type->name ())
2173 error (_("cannot subscript something of type `%s'"),
2174 type->name ());
2175 else
2176 error (_("cannot subscript requested type"));
2177 }
2178 }
2179 }
2180 return (arg1);
2181
2182 case BINOP_LOGICAL_AND:
2183 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2184 if (noside == EVAL_SKIP)
2185 {
2186 evaluate_subexp (nullptr, exp, pos, noside);
2187 return eval_skip_value (exp);
2188 }
2189
2190 oldpos = *pos;
2191 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2192 *pos = oldpos;
2193
2194 if (binop_user_defined_p (op, arg1, arg2))
2195 {
2196 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
2197 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2198 }
2199 else
2200 {
2201 tem = value_logical_not (arg1);
2202 arg2
2203 = evaluate_subexp (nullptr, exp, pos, (tem ? EVAL_SKIP : noside));
2204 type = language_bool_type (exp->language_defn, exp->gdbarch);
2205 return value_from_longest (type,
2206 (LONGEST) (!tem && !value_logical_not (arg2)));
2207 }
2208
2209 case BINOP_LOGICAL_OR:
2210 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2211 if (noside == EVAL_SKIP)
2212 {
2213 evaluate_subexp (nullptr, exp, pos, noside);
2214 return eval_skip_value (exp);
2215 }
2216
2217 oldpos = *pos;
2218 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2219 *pos = oldpos;
2220
2221 if (binop_user_defined_p (op, arg1, arg2))
2222 {
2223 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
2224 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2225 }
2226 else
2227 {
2228 tem = value_logical_not (arg1);
2229 arg2
2230 = evaluate_subexp (nullptr, exp, pos, (!tem ? EVAL_SKIP : noside));
2231 type = language_bool_type (exp->language_defn, exp->gdbarch);
2232 return value_from_longest (type,
2233 (LONGEST) (!tem || !value_logical_not (arg2)));
2234 }
2235
2236 case BINOP_EQUAL:
2237 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2238 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2239 if (noside == EVAL_SKIP)
2240 return eval_skip_value (exp);
2241 if (binop_user_defined_p (op, arg1, arg2))
2242 {
2243 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2244 }
2245 else
2246 {
2247 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2248 tem = value_equal (arg1, arg2);
2249 type = language_bool_type (exp->language_defn, exp->gdbarch);
2250 return value_from_longest (type, (LONGEST) tem);
2251 }
2252
2253 case BINOP_NOTEQUAL:
2254 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2255 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2256 if (noside == EVAL_SKIP)
2257 return eval_skip_value (exp);
2258 if (binop_user_defined_p (op, arg1, arg2))
2259 {
2260 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2261 }
2262 else
2263 {
2264 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2265 tem = value_equal (arg1, arg2);
2266 type = language_bool_type (exp->language_defn, exp->gdbarch);
2267 return value_from_longest (type, (LONGEST) ! tem);
2268 }
2269
2270 case BINOP_LESS:
2271 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2272 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2273 if (noside == EVAL_SKIP)
2274 return eval_skip_value (exp);
2275 if (binop_user_defined_p (op, arg1, arg2))
2276 {
2277 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2278 }
2279 else
2280 {
2281 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2282 tem = value_less (arg1, arg2);
2283 type = language_bool_type (exp->language_defn, exp->gdbarch);
2284 return value_from_longest (type, (LONGEST) tem);
2285 }
2286
2287 case BINOP_GTR:
2288 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2289 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2290 if (noside == EVAL_SKIP)
2291 return eval_skip_value (exp);
2292 if (binop_user_defined_p (op, arg1, arg2))
2293 {
2294 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2295 }
2296 else
2297 {
2298 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2299 tem = value_less (arg2, arg1);
2300 type = language_bool_type (exp->language_defn, exp->gdbarch);
2301 return value_from_longest (type, (LONGEST) tem);
2302 }
2303
2304 case BINOP_GEQ:
2305 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2306 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2307 if (noside == EVAL_SKIP)
2308 return eval_skip_value (exp);
2309 if (binop_user_defined_p (op, arg1, arg2))
2310 {
2311 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2312 }
2313 else
2314 {
2315 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2316 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
2317 type = language_bool_type (exp->language_defn, exp->gdbarch);
2318 return value_from_longest (type, (LONGEST) tem);
2319 }
2320
2321 case BINOP_LEQ:
2322 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2323 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2324 if (noside == EVAL_SKIP)
2325 return eval_skip_value (exp);
2326 if (binop_user_defined_p (op, arg1, arg2))
2327 {
2328 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2329 }
2330 else
2331 {
2332 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2333 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
2334 type = language_bool_type (exp->language_defn, exp->gdbarch);
2335 return value_from_longest (type, (LONGEST) tem);
2336 }
2337
2338 case BINOP_REPEAT:
2339 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2340 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
2341 if (noside == EVAL_SKIP)
2342 return eval_skip_value (exp);
2343 type = check_typedef (value_type (arg2));
2344 if (type->code () != TYPE_CODE_INT
2345 && type->code () != TYPE_CODE_ENUM)
2346 error (_("Non-integral right operand for \"@\" operator."));
2347 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2348 {
2349 return allocate_repeat_value (value_type (arg1),
2350 longest_to_int (value_as_long (arg2)));
2351 }
2352 else
2353 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
2354
2355 case BINOP_COMMA:
2356 evaluate_subexp (nullptr, exp, pos, noside);
2357 return evaluate_subexp (nullptr, exp, pos, noside);
2358
2359 case UNOP_PLUS:
2360 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2361 if (noside == EVAL_SKIP)
2362 return eval_skip_value (exp);
2363 if (unop_user_defined_p (op, arg1))
2364 return value_x_unop (arg1, op, noside);
2365 else
2366 {
2367 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2368 return value_pos (arg1);
2369 }
2370
2371 case UNOP_NEG:
2372 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2373 if (noside == EVAL_SKIP)
2374 return eval_skip_value (exp);
2375 if (unop_user_defined_p (op, arg1))
2376 return value_x_unop (arg1, op, noside);
2377 else
2378 {
2379 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2380 return value_neg (arg1);
2381 }
2382
2383 case UNOP_COMPLEMENT:
2384 /* C++: check for and handle destructor names. */
2385
2386 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2387 if (noside == EVAL_SKIP)
2388 return eval_skip_value (exp);
2389 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
2390 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
2391 else
2392 {
2393 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2394 return value_complement (arg1);
2395 }
2396
2397 case UNOP_LOGICAL_NOT:
2398 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2399 if (noside == EVAL_SKIP)
2400 return eval_skip_value (exp);
2401 if (unop_user_defined_p (op, arg1))
2402 return value_x_unop (arg1, op, noside);
2403 else
2404 {
2405 type = language_bool_type (exp->language_defn, exp->gdbarch);
2406 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
2407 }
2408
2409 case UNOP_IND:
2410 if (expect_type && expect_type->code () == TYPE_CODE_PTR)
2411 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
2412 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2413 type = check_typedef (value_type (arg1));
2414 if (type->code () == TYPE_CODE_METHODPTR
2415 || type->code () == TYPE_CODE_MEMBERPTR)
2416 error (_("Attempt to dereference pointer "
2417 "to member without an object"));
2418 if (noside == EVAL_SKIP)
2419 return eval_skip_value (exp);
2420 if (unop_user_defined_p (op, arg1))
2421 return value_x_unop (arg1, op, noside);
2422 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2423 {
2424 type = check_typedef (value_type (arg1));
2425 if (type->code () == TYPE_CODE_PTR
2426 || TYPE_IS_REFERENCE (type)
2427 /* In C you can dereference an array to get the 1st elt. */
2428 || type->code () == TYPE_CODE_ARRAY
2429 )
2430 return value_zero (TYPE_TARGET_TYPE (type),
2431 lval_memory);
2432 else if (type->code () == TYPE_CODE_INT)
2433 /* GDB allows dereferencing an int. */
2434 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
2435 lval_memory);
2436 else
2437 error (_("Attempt to take contents of a non-pointer value."));
2438 }
2439
2440 /* Allow * on an integer so we can cast it to whatever we want.
2441 This returns an int, which seems like the most C-like thing to
2442 do. "long long" variables are rare enough that
2443 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
2444 if (type->code () == TYPE_CODE_INT)
2445 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
2446 (CORE_ADDR) value_as_address (arg1));
2447 return value_ind (arg1);
2448
2449 case UNOP_ADDR:
2450 /* C++: check for and handle pointer to members. */
2451
2452 if (noside == EVAL_SKIP)
2453 {
2454 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
2455 return eval_skip_value (exp);
2456 }
2457 else
2458 return evaluate_subexp_for_address (exp, pos, noside);
2459
2460 case UNOP_SIZEOF:
2461 if (noside == EVAL_SKIP)
2462 {
2463 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
2464 return eval_skip_value (exp);
2465 }
2466 return evaluate_subexp_for_sizeof (exp, pos, noside);
2467
2468 case UNOP_ALIGNOF:
2469 {
2470 type = value_type (
2471 evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS));
2472 /* FIXME: This should be size_t. */
2473 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2474 ULONGEST align = type_align (type);
2475 if (align == 0)
2476 error (_("could not determine alignment of type"));
2477 return value_from_longest (size_type, align);
2478 }
2479
2480 case UNOP_CAST:
2481 (*pos) += 2;
2482 type = exp->elts[pc + 1].type;
2483 return evaluate_subexp_for_cast (exp, pos, noside, type);
2484
2485 case UNOP_CAST_TYPE:
2486 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2487 type = value_type (arg1);
2488 return evaluate_subexp_for_cast (exp, pos, noside, type);
2489
2490 case UNOP_DYNAMIC_CAST:
2491 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2492 type = value_type (arg1);
2493 arg1 = evaluate_subexp (type, exp, pos, noside);
2494 if (noside == EVAL_SKIP)
2495 return eval_skip_value (exp);
2496 return value_dynamic_cast (type, arg1);
2497
2498 case UNOP_REINTERPRET_CAST:
2499 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2500 type = value_type (arg1);
2501 arg1 = evaluate_subexp (type, exp, pos, noside);
2502 if (noside == EVAL_SKIP)
2503 return eval_skip_value (exp);
2504 return value_reinterpret_cast (type, arg1);
2505
2506 case UNOP_MEMVAL:
2507 (*pos) += 2;
2508 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2509 if (noside == EVAL_SKIP)
2510 return eval_skip_value (exp);
2511 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2512 return value_zero (exp->elts[pc + 1].type, lval_memory);
2513 else
2514 return value_at_lazy (exp->elts[pc + 1].type,
2515 value_as_address (arg1));
2516
2517 case UNOP_MEMVAL_TYPE:
2518 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2519 type = value_type (arg1);
2520 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2521 if (noside == EVAL_SKIP)
2522 return eval_skip_value (exp);
2523 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2524 return value_zero (type, lval_memory);
2525 else
2526 return value_at_lazy (type, value_as_address (arg1));
2527
2528 case UNOP_PREINCREMENT:
2529 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2530 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2531 return arg1;
2532 else if (unop_user_defined_p (op, arg1))
2533 {
2534 return value_x_unop (arg1, op, noside);
2535 }
2536 else
2537 {
2538 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2539 arg2 = value_ptradd (arg1, 1);
2540 else
2541 {
2542 struct value *tmp = arg1;
2543
2544 arg2 = value_one (value_type (arg1));
2545 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2546 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2547 }
2548
2549 return value_assign (arg1, arg2);
2550 }
2551
2552 case UNOP_PREDECREMENT:
2553 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2554 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2555 return arg1;
2556 else if (unop_user_defined_p (op, arg1))
2557 {
2558 return value_x_unop (arg1, op, noside);
2559 }
2560 else
2561 {
2562 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2563 arg2 = value_ptradd (arg1, -1);
2564 else
2565 {
2566 struct value *tmp = arg1;
2567
2568 arg2 = value_one (value_type (arg1));
2569 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2570 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2571 }
2572
2573 return value_assign (arg1, arg2);
2574 }
2575
2576 case UNOP_POSTINCREMENT:
2577 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2578 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2579 return arg1;
2580 else if (unop_user_defined_p (op, arg1))
2581 {
2582 return value_x_unop (arg1, op, noside);
2583 }
2584 else
2585 {
2586 arg3 = value_non_lval (arg1);
2587
2588 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2589 arg2 = value_ptradd (arg1, 1);
2590 else
2591 {
2592 struct value *tmp = arg1;
2593
2594 arg2 = value_one (value_type (arg1));
2595 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2596 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2597 }
2598
2599 value_assign (arg1, arg2);
2600 return arg3;
2601 }
2602
2603 case UNOP_POSTDECREMENT:
2604 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2605 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2606 return arg1;
2607 else if (unop_user_defined_p (op, arg1))
2608 {
2609 return value_x_unop (arg1, op, noside);
2610 }
2611 else
2612 {
2613 arg3 = value_non_lval (arg1);
2614
2615 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2616 arg2 = value_ptradd (arg1, -1);
2617 else
2618 {
2619 struct value *tmp = arg1;
2620
2621 arg2 = value_one (value_type (arg1));
2622 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2623 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2624 }
2625
2626 value_assign (arg1, arg2);
2627 return arg3;
2628 }
2629
2630 case OP_THIS:
2631 (*pos) += 1;
2632 return value_of_this (exp->language_defn);
2633
2634 case OP_TYPE:
2635 /* The value is not supposed to be used. This is here to make it
2636 easier to accommodate expressions that contain types. */
2637 (*pos) += 2;
2638 if (noside == EVAL_SKIP)
2639 return eval_skip_value (exp);
2640 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2641 return allocate_value (exp->elts[pc + 1].type);
2642 else
2643 error (_("Attempt to use a type name as an expression"));
2644
2645 case OP_TYPEOF:
2646 case OP_DECLTYPE:
2647 if (noside == EVAL_SKIP)
2648 {
2649 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
2650 return eval_skip_value (exp);
2651 }
2652 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2653 {
2654 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2655 struct value *result;
2656
2657 result = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2658
2659 /* 'decltype' has special semantics for lvalues. */
2660 if (op == OP_DECLTYPE
2661 && (sub_op == BINOP_SUBSCRIPT
2662 || sub_op == STRUCTOP_MEMBER
2663 || sub_op == STRUCTOP_MPTR
2664 || sub_op == UNOP_IND
2665 || sub_op == STRUCTOP_STRUCT
2666 || sub_op == STRUCTOP_PTR
2667 || sub_op == OP_SCOPE))
2668 {
2669 type = value_type (result);
2670
2671 if (!TYPE_IS_REFERENCE (type))
2672 {
2673 type = lookup_lvalue_reference_type (type);
2674 result = allocate_value (type);
2675 }
2676 }
2677
2678 return result;
2679 }
2680 else
2681 error (_("Attempt to use a type as an expression"));
2682
2683 case OP_TYPEID:
2684 {
2685 struct value *result;
2686 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2687
2688 if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
2689 result = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2690 else
2691 result = evaluate_subexp (nullptr, exp, pos, noside);
2692
2693 if (noside != EVAL_NORMAL)
2694 return allocate_value (cplus_typeid_type (exp->gdbarch));
2695
2696 return cplus_typeid (result);
2697 }
2698
2699 default:
2700 /* Removing this case and compiling with gcc -Wall reveals that
2701 a lot of cases are hitting this case. Some of these should
2702 probably be removed from expression.h; others are legitimate
2703 expressions which are (apparently) not fully implemented.
2704
2705 If there are any cases landing here which mean a user error,
2706 then they should be separate cases, with more descriptive
2707 error messages. */
2708
2709 error (_("GDB does not (yet) know how to "
2710 "evaluate that kind of expression"));
2711 }
2712
2713 gdb_assert_not_reached ("missed return?");
2714 }
2715 \f
2716 /* Evaluate a subexpression of EXP, at index *POS,
2717 and return the address of that subexpression.
2718 Advance *POS over the subexpression.
2719 If the subexpression isn't an lvalue, get an error.
2720 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2721 then only the type of the result need be correct. */
2722
2723 static struct value *
2724 evaluate_subexp_for_address (struct expression *exp, int *pos,
2725 enum noside noside)
2726 {
2727 enum exp_opcode op;
2728 int pc;
2729 struct symbol *var;
2730 struct value *x;
2731 int tem;
2732
2733 pc = (*pos);
2734 op = exp->elts[pc].opcode;
2735
2736 switch (op)
2737 {
2738 case UNOP_IND:
2739 (*pos)++;
2740 x = evaluate_subexp (nullptr, exp, pos, noside);
2741
2742 /* We can't optimize out "&*" if there's a user-defined operator*. */
2743 if (unop_user_defined_p (op, x))
2744 {
2745 x = value_x_unop (x, op, noside);
2746 goto default_case_after_eval;
2747 }
2748
2749 return coerce_array (x);
2750
2751 case UNOP_MEMVAL:
2752 (*pos) += 3;
2753 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
2754 evaluate_subexp (nullptr, exp, pos, noside));
2755
2756 case UNOP_MEMVAL_TYPE:
2757 {
2758 struct type *type;
2759
2760 (*pos) += 1;
2761 x = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2762 type = value_type (x);
2763 return value_cast (lookup_pointer_type (type),
2764 evaluate_subexp (nullptr, exp, pos, noside));
2765 }
2766
2767 case OP_VAR_VALUE:
2768 var = exp->elts[pc + 2].symbol;
2769
2770 /* C++: The "address" of a reference should yield the address
2771 * of the object pointed to. Let value_addr() deal with it. */
2772 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var)))
2773 goto default_case;
2774
2775 (*pos) += 4;
2776 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2777 {
2778 struct type *type =
2779 lookup_pointer_type (SYMBOL_TYPE (var));
2780 enum address_class sym_class = SYMBOL_CLASS (var);
2781
2782 if (sym_class == LOC_CONST
2783 || sym_class == LOC_CONST_BYTES
2784 || sym_class == LOC_REGISTER)
2785 error (_("Attempt to take address of register or constant."));
2786
2787 return
2788 value_zero (type, not_lval);
2789 }
2790 else
2791 return address_of_variable (var, exp->elts[pc + 1].block);
2792
2793 case OP_VAR_MSYM_VALUE:
2794 {
2795 (*pos) += 4;
2796
2797 value *val = evaluate_var_msym_value (noside,
2798 exp->elts[pc + 1].objfile,
2799 exp->elts[pc + 2].msymbol);
2800 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2801 {
2802 struct type *type = lookup_pointer_type (value_type (val));
2803 return value_zero (type, not_lval);
2804 }
2805 else
2806 return value_addr (val);
2807 }
2808
2809 case OP_SCOPE:
2810 tem = longest_to_int (exp->elts[pc + 2].longconst);
2811 (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1);
2812 x = value_aggregate_elt (exp->elts[pc + 1].type,
2813 &exp->elts[pc + 3].string,
2814 NULL, 1, noside);
2815 if (x == NULL)
2816 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
2817 return x;
2818
2819 default:
2820 default_case:
2821 x = evaluate_subexp (nullptr, exp, pos, noside);
2822 default_case_after_eval:
2823 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2824 {
2825 struct type *type = check_typedef (value_type (x));
2826
2827 if (TYPE_IS_REFERENCE (type))
2828 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2829 not_lval);
2830 else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
2831 return value_zero (lookup_pointer_type (value_type (x)),
2832 not_lval);
2833 else
2834 error (_("Attempt to take address of "
2835 "value not located in memory."));
2836 }
2837 return value_addr (x);
2838 }
2839 }
2840
2841 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
2842 When used in contexts where arrays will be coerced anyway, this is
2843 equivalent to `evaluate_subexp' but much faster because it avoids
2844 actually fetching array contents (perhaps obsolete now that we have
2845 value_lazy()).
2846
2847 Note that we currently only do the coercion for C expressions, where
2848 arrays are zero based and the coercion is correct. For other languages,
2849 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
2850 to decide if coercion is appropriate. */
2851
2852 struct value *
2853 evaluate_subexp_with_coercion (struct expression *exp,
2854 int *pos, enum noside noside)
2855 {
2856 enum exp_opcode op;
2857 int pc;
2858 struct value *val;
2859 struct symbol *var;
2860 struct type *type;
2861
2862 pc = (*pos);
2863 op = exp->elts[pc].opcode;
2864
2865 switch (op)
2866 {
2867 case OP_VAR_VALUE:
2868 var = exp->elts[pc + 2].symbol;
2869 type = check_typedef (SYMBOL_TYPE (var));
2870 if (type->code () == TYPE_CODE_ARRAY
2871 && !type->is_vector ()
2872 && CAST_IS_CONVERSION (exp->language_defn))
2873 {
2874 (*pos) += 4;
2875 val = address_of_variable (var, exp->elts[pc + 1].block);
2876 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2877 val);
2878 }
2879 /* FALLTHROUGH */
2880
2881 default:
2882 return evaluate_subexp (nullptr, exp, pos, noside);
2883 }
2884 }
2885
2886 /* Evaluate a subexpression of EXP, at index *POS,
2887 and return a value for the size of that subexpression.
2888 Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
2889 we allow side-effects on the operand if its type is a variable
2890 length array. */
2891
2892 static struct value *
2893 evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
2894 enum noside noside)
2895 {
2896 /* FIXME: This should be size_t. */
2897 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2898 enum exp_opcode op;
2899 int pc;
2900 struct type *type;
2901 struct value *val;
2902
2903 pc = (*pos);
2904 op = exp->elts[pc].opcode;
2905
2906 switch (op)
2907 {
2908 /* This case is handled specially
2909 so that we avoid creating a value for the result type.
2910 If the result type is very big, it's desirable not to
2911 create a value unnecessarily. */
2912 case UNOP_IND:
2913 (*pos)++;
2914 val = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2915 type = check_typedef (value_type (val));
2916 if (type->code () != TYPE_CODE_PTR
2917 && !TYPE_IS_REFERENCE (type)
2918 && type->code () != TYPE_CODE_ARRAY)
2919 error (_("Attempt to take contents of a non-pointer value."));
2920 type = TYPE_TARGET_TYPE (type);
2921 if (is_dynamic_type (type))
2922 type = value_type (value_ind (val));
2923 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
2924
2925 case UNOP_MEMVAL:
2926 (*pos) += 3;
2927 type = exp->elts[pc + 1].type;
2928 break;
2929
2930 case UNOP_MEMVAL_TYPE:
2931 (*pos) += 1;
2932 val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2933 type = value_type (val);
2934 break;
2935
2936 case OP_VAR_VALUE:
2937 type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
2938 if (is_dynamic_type (type))
2939 {
2940 val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL);
2941 type = value_type (val);
2942 if (type->code () == TYPE_CODE_ARRAY)
2943 {
2944 if (type_not_allocated (type) || type_not_associated (type))
2945 return value_zero (size_type, not_lval);
2946 else if (is_dynamic_type (type->index_type ())
2947 && type->bounds ()->high.kind () == PROP_UNDEFINED)
2948 return allocate_optimized_out_value (size_type);
2949 }
2950 }
2951 else
2952 (*pos) += 4;
2953 break;
2954
2955 case OP_VAR_MSYM_VALUE:
2956 {
2957 (*pos) += 4;
2958
2959 minimal_symbol *msymbol = exp->elts[pc + 2].msymbol;
2960 value *mval = evaluate_var_msym_value (noside,
2961 exp->elts[pc + 1].objfile,
2962 msymbol);
2963
2964 type = value_type (mval);
2965 if (type->code () == TYPE_CODE_ERROR)
2966 error_unknown_type (msymbol->print_name ());
2967
2968 return value_from_longest (size_type, TYPE_LENGTH (type));
2969 }
2970 break;
2971
2972 /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
2973 type of the subscript is a variable length array type. In this case we
2974 must re-evaluate the right hand side of the subscription to allow
2975 side-effects. */
2976 case BINOP_SUBSCRIPT:
2977 if (noside == EVAL_NORMAL)
2978 {
2979 int npc = (*pos) + 1;
2980
2981 val = evaluate_subexp (nullptr, exp, &npc, EVAL_AVOID_SIDE_EFFECTS);
2982 type = check_typedef (value_type (val));
2983 if (type->code () == TYPE_CODE_ARRAY)
2984 {
2985 type = check_typedef (TYPE_TARGET_TYPE (type));
2986 if (type->code () == TYPE_CODE_ARRAY)
2987 {
2988 type = type->index_type ();
2989 /* Only re-evaluate the right hand side if the resulting type
2990 is a variable length type. */
2991 if (type->bounds ()->flag_bound_evaluated)
2992 {
2993 val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL);
2994 return value_from_longest
2995 (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
2996 }
2997 }
2998 }
2999 }
3000
3001 /* Fall through. */
3002
3003 default:
3004 val = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3005 type = value_type (val);
3006 break;
3007 }
3008
3009 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
3010 "When applied to a reference or a reference type, the result is
3011 the size of the referenced type." */
3012 type = check_typedef (type);
3013 if (exp->language_defn->la_language == language_cplus
3014 && (TYPE_IS_REFERENCE (type)))
3015 type = check_typedef (TYPE_TARGET_TYPE (type));
3016 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
3017 }
3018
3019 /* Evaluate a subexpression of EXP, at index *POS, and return a value
3020 for that subexpression cast to TO_TYPE. Advance *POS over the
3021 subexpression. */
3022
3023 static value *
3024 evaluate_subexp_for_cast (expression *exp, int *pos,
3025 enum noside noside,
3026 struct type *to_type)
3027 {
3028 int pc = *pos;
3029
3030 /* Don't let symbols be evaluated with evaluate_subexp because that
3031 throws an "unknown type" error for no-debug data symbols.
3032 Instead, we want the cast to reinterpret the symbol. */
3033 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE
3034 || exp->elts[pc].opcode == OP_VAR_VALUE)
3035 {
3036 (*pos) += 4;
3037
3038 value *val;
3039 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE)
3040 {
3041 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3042 return value_zero (to_type, not_lval);
3043
3044 val = evaluate_var_msym_value (noside,
3045 exp->elts[pc + 1].objfile,
3046 exp->elts[pc + 2].msymbol);
3047 }
3048 else
3049 val = evaluate_var_value (noside,
3050 exp->elts[pc + 1].block,
3051 exp->elts[pc + 2].symbol);
3052
3053 if (noside == EVAL_SKIP)
3054 return eval_skip_value (exp);
3055
3056 val = value_cast (to_type, val);
3057
3058 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
3059 if (VALUE_LVAL (val) == lval_memory)
3060 {
3061 if (value_lazy (val))
3062 value_fetch_lazy (val);
3063 VALUE_LVAL (val) = not_lval;
3064 }
3065 return val;
3066 }
3067
3068 value *val = evaluate_subexp (to_type, exp, pos, noside);
3069 if (noside == EVAL_SKIP)
3070 return eval_skip_value (exp);
3071 return value_cast (to_type, val);
3072 }
3073
3074 /* Parse a type expression in the string [P..P+LENGTH). */
3075
3076 struct type *
3077 parse_and_eval_type (const char *p, int length)
3078 {
3079 char *tmp = (char *) alloca (length + 4);
3080
3081 tmp[0] = '(';
3082 memcpy (tmp + 1, p, length);
3083 tmp[length + 1] = ')';
3084 tmp[length + 2] = '0';
3085 tmp[length + 3] = '\0';
3086 expression_up expr = parse_expression (tmp);
3087 if (expr->first_opcode () != UNOP_CAST)
3088 error (_("Internal error in eval_type."));
3089 return expr->elts[1].type;
3090 }
This page took 0.089436 seconds and 5 git commands to generate.