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