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